• SO MUCH Better than delegate protocols! • iOS 5.0 SDK started to include ‘callback blocks’ in api calls instead of delegate protocols! • GCD made easy: dispatch_async()! -
of API calls that use callbacks. • AFNetworking becomes the #1 Objective-C library! Cause it uses callbacks instead of horrible NSURLConnection delegates! • We can get our stuff out of the main q (dispatch_async())
callback run? Is it in a background queue? main queue? Could it be either? Do I have to add my own defensive dispatch_async? • 2 - Error Handling? Two different callbacks? Enumerations? • 3 - What if I cancel? Should my callback still be called? Do I get an error? (Never been consistent..)
returned from a function, instead of a callback. TypeSafe. A consumer can then “attach” completion blocks to create even more interesting Futures. • Promise - the Producer interface. Is used to create a Future. Think - “Always keep your Promises”. (Always complete your promises, or your code will hang). Can be used to ‘wrap’ any existing callback code and convert it into a Future.
in switch result { case let .Success(value): return .Success(String(value)) case let .Fail(err): return .Fail(err) case .Cancelled: return .Cancelled } }
.onSuccess { (stringResult:String) -> Int in let i = Int(stringResult)! return i } .map { intResult -> [Int] in let array = [intResult] return array } .onSuccess { arrayResult -> Int? in return arrayResult.first! }
{ // let's dispatch this to the low priority background queue return Future(.Background) { () -> Int in let ret = num + 1 return ret } } let stringFuture = Future<Int>(success: 5) stringFuture .onSuccess { intResult -> Future<Int> in return coolFunctionThatAddsOneInBackground(intResult) } .onSuccess { return coolFunctionThatAddsOneInBackground($0) }
let namesFuture :Future<[String]> = namesPromise.future namesFuture.onSuccess(.Main) { (names : [String]) -> Void in for name in names { print("Happy Future Day \(name)!") } }
‘fetches’ a ‘Model’ object from server. • Make an API call (Networking Errors). Parse JSON (Json Parsing errors) Save to the DB (File IO Errors, DB Validation Errors). • As you add more logic, you will have more potential errors.
it’s ErrorType.. undermines composability. • By forcing consumers to ‘expect’ any error, it means you can modify any Futures. • side effect: You MUST add error handling.
Use with care, but good for simple value-mapping. .MainImmediate - Will try to avoid unneeded dispatch_async if already in the Main .MainAsync - always does a dispatch_async() .Queue(q) - your own custom dispatch_queue .OperationQueue(opq) - wraps NSOperationQueue .ManagedObjectContext(c) - wraps ‘performBlock()’
currently running! (If not running in Executor, than maps to .Main or .Async). May not work correctly with .Custom • .Primary = The Context to use if you don’t define one (default = .Current) Configurable • .Main = .MainImmediate/.MainAsync (default: .MainImmediate). • .Async = Configurable ‘not-main-queue’. Default: .Default (qos default) • .Custom = build your own!
single ‘future’ property that returns a result. • Need more than one type of result? Use an enumerated value!. • example Model viewControllers that return their results via a Future.