Skip to content

Instantly share code, notes, and snippets.

@juliengdt
Last active November 19, 2020 09:10
Show Gist options
  • Select an option

  • Save juliengdt/4e27cd1bf4fe3db70144cbdffd86af86 to your computer and use it in GitHub Desktop.

Select an option

Save juliengdt/4e27cd1bf4fe3db70144cbdffd86af86 to your computer and use it in GitHub Desktop.

Revisions

  1. juliengdt revised this gist Nov 19, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Dowit.swift
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ Do.ui(on: self) { this in



    /// A nano class which help developer avoid messy code with DispatchQueue and `self` weaked pointed
    /// A nano class which help developer avoid messy code with DispatchQueue and `self` weaked pointer
    final class Do {

    typealias DoClosure<T> = ((_:T)->()) // just because it's horrible to read
  2. juliengdt revised this gist Nov 19, 2020. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion Dowit.swift
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,17 @@ import Foundation

    /// #Do class - Or how to transform DispatchQueue-escaping-weakable stuff on pointer into a more readable usage

    TL;DR
    /// TL;DR - Usage

    // In ViewModel/Interactor
    Do.bg(on: self) { this in
    this.doSomeWSCalls()
    }

    // In View(Controller
    Do.ui(on: self) { this in
    this.reloadView()
    }



  3. juliengdt revised this gist Nov 19, 2020. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions Dowit.swift
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,11 @@ import Foundation

    /// #Do class - Or how to transform DispatchQueue-escaping-weakable stuff on pointer into a more readable usage

    TL;DR




    /// A nano class which help developer avoid messy code with DispatchQueue and `self` weaked pointed
    final class Do {

  4. juliengdt renamed this gist Nov 18, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. juliengdt revised this gist Nov 18, 2020. 1 changed file with 45 additions and 8 deletions.
    53 changes: 45 additions & 8 deletions Playground.swift
    Original file line number Diff line number Diff line change
    @@ -32,21 +32,19 @@ final class Do {

    /// ## From this ...



    class MyClass {
    var string: String = "Oh, Hello !"
    func doSomethingOnScreen() {
    DispatchQueue.main.async { [weak self] in
    Thread.printCurrentDescription()
    print(self?.string ?? "borken pointer")
    print("Done things in the background.")
    }
    }

    func doSomethingInBackground() {
    DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async { [weak self] in
    Thread.printCurrentDescription()
    self?.string = "Oh, Hello from the background !"
    print("Done things in the background.")
    }
    }

    @@ -56,6 +54,46 @@ class MyClass {
    /// ## ... to this

    class MyBetterClass {
    var string: String = "Oh, Hello !"
    func doSomethingOnScreen() {
    Do.ui(on: self) { this in
    print(this.string)
    }
    }

    func doSomethingInBackground() {
    Do.bg(on: self) { this in
    this.string = "Oh, Hello from the background ! (But better 😎)"
    }
    }

    }



    /// Usage

    class _MyClass {
    var string: String = "Oh, Hello !"
    func doSomethingOnScreen() {
    DispatchQueue.main.async { [weak self] in
    Thread.printCurrentDescription()
    print(self?.string ?? "borken pointer")
    print("Done things in the background.")
    }
    }

    func doSomethingInBackground() {
    DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async { [weak self] in
    Thread.printCurrentDescription()
    self?.string = "Oh, Hello from the background !"
    print("Done things in the background.")
    }
    }

    }

    class _MyBetterClass {
    var string: String = "Oh, Hello !"
    func doSomethingOnScreen() {
    Do.ui(on: self) { this in
    @@ -76,10 +114,9 @@ class MyBetterClass {



    /// Usage

    // ⚠️ -- debug print are biased (their order) due to print latency ...

    let boringClass = MyClass()
    let boringClass = _MyClass()
    boringClass.doSomethingInBackground()
    do {
    sleep(1)
    @@ -90,7 +127,7 @@ do {
    sleep(2)
    }

    let betterClass = MyBetterClass()
    let betterClass = _MyBetterClass()
    betterClass.doSomethingInBackground()
    do {
    sleep(1)
  6. juliengdt created this gist Nov 18, 2020.
    106 changes: 106 additions & 0 deletions Playground.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    import Foundation


    /// #Do class - Or how to transform DispatchQueue-escaping-weakable stuff on pointer into a more readable usage

    /// A nano class which help developer avoid messy code with DispatchQueue and `self` weaked pointed
    final class Do {

    typealias DoClosure<T> = ((_:T)->()) // just because it's horrible to read

    private init() {}

    /// Do it in background thread
    static func bg<T: AnyObject>(on object: T, closure: @escaping DoClosure<T>) {
    weak var _object = object
    DispatchQueue.global(qos: .background).async {
    guard let __obj = _object else { return }
    closure(__obj)
    }
    }

    /// Do it in main thread
    static func ui<T: AnyObject>(on object: T, closure: @escaping DoClosure<T>) {
    weak var _object = object
    DispatchQueue.main.async {
    guard let __obj = _object else { return }
    closure(__obj)
    }
    }

    }

    /// ## From this ...

    class MyClass {
    var string: String = "Oh, Hello !"
    func doSomethingOnScreen() {
    DispatchQueue.main.async { [weak self] in
    Thread.printCurrentDescription()
    print(self?.string ?? "borken pointer")
    print("Done things in the background.")
    }
    }

    func doSomethingInBackground() {
    DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async { [weak self] in
    Thread.printCurrentDescription()
    self?.string = "Oh, Hello from the background !"
    print("Done things in the background.")
    }
    }

    }


    /// ## ... to this

    class MyBetterClass {
    var string: String = "Oh, Hello !"
    func doSomethingOnScreen() {
    Do.ui(on: self) { this in
    Thread.printCurrentDescription()
    print(this.string)
    print("Done things in the background. (But better 😎)")
    }
    }

    func doSomethingInBackground() {
    Do.bg(on: self) { this in
    Thread.printCurrentDescription()
    this.string = "Oh, Hello from the background ! (But better 😎)"
    }
    }

    }



    /// Usage


    let boringClass = MyClass()
    boringClass.doSomethingInBackground()
    do {
    sleep(1)
    }
    boringClass.doSomethingOnScreen()

    do {
    sleep(2)
    }

    let betterClass = MyBetterClass()
    betterClass.doSomethingInBackground()
    do {
    sleep(1)
    }
    betterClass.doSomethingOnScreen()


    /// Just a helper extension πŸ‘€
    extension Thread {
    class func printCurrentDescription() {
    print("\r⚑️: \(Thread.current)\r" + "🏭: \(OperationQueue.current?.underlyingQueue?.label ?? "None")\r")
    }
    }