Last active
August 29, 2015 14:07
-
-
Save morizotter/7652e13b9a2f2023373e to your computer and use it in GitHub Desktop.
Bolts-iOSのとっても簡単な使い方説明 ref: http://qiita.com/morizotter/items/8fcd8017b938927f6c4a
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 characters
pod 'Bolts', '~> 1.1' |
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 characters
pod install |
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 characters
#import <Bolts.h> |
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 characters
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// PREPARE EMPTY TASK - FOR EXECUTING TASKS IN SERIES | |
var task = BFTask(result: nil) | |
// 1. ASYNCRONOUS CONNECTION TO YAHOO | |
task = task.continueWithBlock({ (task: BFTask!) -> BFTask! in | |
// INSTANTIATE TASK COMPLETION | |
let taskCompletion = BFTaskCompletionSource() | |
// ASYNCRONOUS NETWORK ACTIVITY | |
let url = "http://yahoo.co.jp" | |
let session = NSURLSession.sharedSession() | |
let sessionTask = session.dataTaskWithURL(NSURL(string: url), completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in | |
if let err = error { | |
println("1 ERROR: YAHOO!") | |
taskCompletion.setError(err) | |
} | |
println("1 SUCCESS: YAHOO!") | |
taskCompletion.setResult(data) | |
}) | |
sessionTask.resume() | |
// RETURN TASK | |
return taskCompletion.task | |
}) | |
// 2. ASYNCRONOUS CONNECTION TO GOOGLE | |
task = task.continueWithBlock({ (task: BFTask!) -> BFTask! in | |
// INSTANTIATE TASK COMPLETION | |
let taskCompletion = BFTaskCompletionSource() | |
// ASYNCRONOUS NETWORK ACTIVITY | |
let url = "http://google.com" | |
let session = NSURLSession.sharedSession() | |
let sessionTask = session.dataTaskWithURL(NSURL(string: url), completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in | |
if let err = error { | |
println("2 ERROR: GOOGLE!") | |
taskCompletion.setError(err) | |
} | |
println("2 SUCCESS: GOOGLE!") | |
taskCompletion.setResult(data) | |
}) | |
sessionTask.resume() | |
// RETURN TASK | |
return taskCompletion.task | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment