Bolts

20
Bolts Superbil @Cocoaheads Taipei

Transcript of Bolts

Page 1: Bolts

BoltsSuperbil @Cocoaheads Taipei

Page 2: Bolts

About me

• 燒毀果蠅的蘋果園藝師

• Objective-C / Git / Emacs / Python

• about.me/superbil , @superbil

• freenode #g0v.tw #emacs.tw

• Freelance Software Developer

Page 3: Bolts

Guideline

• Functional Reactive Programming

• Bolts Intro

• Why Bolts?

• Example

Page 4: Bolts

Functional Reactive Programming

• 讓資料流變化可以⾃自動傳播的程式設計典範FRP與函數式 http://www.ithome.com.tw/voice/91328

Page 5: Bolts

Why Bolts ?

• Facebook

• github.com/zonble/CWBOpenDataClient

Page 6: Bolts

Why Bolts?

• NSRunloop

• input source

• Timer sources

mikeash NSRunLoop Internals https://mikeash.com/pyblog/friday-qa-2010-01-01-nsrunloop-internals.html

Page 7: Bolts

Bolts Intro

• A task is kind of like a JavaScript Promise

https://www.promisejs.org/

Page 8: Bolts

Bolts Intro

• Property: data sequence than remembers only the last value

• a property with only one single value/event

• Promises are a subset of FRP

Taming Asynchronous Programming (beyond Promises) http://eamodeorubio.github.io/tamingasync/#/60

Page 9: Bolts

Promise

• must have value (callback)

• result

• error

• etc..

Page 10: Bolts

contiuneWithBlock:

[[self saveAsync:obj] continueWithBlock:^id(BFTask *task) { if (task.isCancelled) { // the save was cancelled. } else if (task.error) { // the save failed. } else { // the object was saved successfully. PFObject *object = task.result; } return nil; }];

Page 11: Bolts

continueWithExecutor:withBlock:

// Continue on the Main Thread, using a built-in executor. [[self fetchAsync:object] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) { myTextView.text = [object objectForKey:@"name"]; }];

Page 12: Bolts

nil is end

[[self saveAsync:obj] continueWithSuccessBlock:^id(BFTask *task) { // the object was saved successfully. return nil; }];

Page 13: Bolts

BFTaskCompletionSource

• Normal use

• setResult:

• setError:

• setException:

• cancel

• Try to set

• trySetResult

• trySetError

• trySetException

• trySetCancelled

Page 14: Bolts

BFExecutor

• defaultExecutor

• mainThreadExecutor

• dispatch_queue_t

• NSOperationQueue

Page 15: Bolts

for completion

• taskForCompletionOfAllTasks:

• taskForCompletionOfAllTasksWithResults:

Page 16: Bolts

BFCancellationToken

• Solve flow need cancel problem

• After 1.2.0 support

Page 17: Bolts

BFCancellationTokenBFCancellationTokenSource *cts = [BFCancellationTokenSource cancellationTokenSource]; [cts.token registerCancellationObserverWithBlock:^{ NSLog(@"A"); }]; BFTask *task = [BFTask taskWithDelay:500]; [task continueWithBlock:^id(BFTask *task) { NSLog(@"B"); return nil; } cancellationToken:cts.token]; NSLog(@“C");

[cts cancel];

Page 18: Bolts

Live DEMO

Page 19: Bolts

More example

• https://github.com/BoltsFramework/Bolts-iOS

• Readme.md

• TaskTests.m

• CancellationTests.m

Page 20: Bolts

Q & A