Last active
November 19, 2020 09:10
-
-
Save juliengdt/4e27cd1bf4fe3db70144cbdffd86af86 to your computer and use it in GitHub Desktop.
Revisions
-
juliengdt revised this gist
Nov 19, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 pointer final class Do { typealias DoClosure<T> = ((_:T)->()) // just because it's horrible to read -
juliengdt revised this gist
Nov 19, 2020 . 1 changed file with 10 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 - Usage // In ViewModel/Interactor Do.bg(on: self) { this in this.doSomeWSCalls() } // In View(Controller Do.ui(on: self) { this in this.reloadView() } -
juliengdt revised this gist
Nov 19, 2020 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 { -
juliengdt renamed this gist
Nov 18, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
juliengdt revised this gist
Nov 18, 2020 . 1 changed file with 45 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 print(self?.string ?? "borken pointer") } } func doSomethingInBackground() { DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async { [weak self] in self?.string = "Oh, Hello from 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 { // β οΈ -- debug print are biased (their order) due to print latency ... let boringClass = _MyClass() boringClass.doSomethingInBackground() do { sleep(1) @@ -90,7 +127,7 @@ do { sleep(2) } let betterClass = _MyBetterClass() betterClass.doSomethingInBackground() do { sleep(1) -
juliengdt created this gist
Nov 18, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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") } }