Created
September 8, 2015 13:20
-
-
Save nRewik/f72cbc7e087c447421da to your computer and use it in GitHub Desktop.
how request chaining work with NSUrlSessionDelegate
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
//: Playground - noun: a place where people can play | |
import UIKit | |
import XCPlayground | |
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true) | |
class ExampleChainingRequest: NSObject{ | |
private var session: NSURLSession! | |
private let loginURL = "http://my.server.com/token" | |
private let getUserDataURL = "http://my.server.com/user" | |
private let getAddressesURL = "http://my.server.com/addresses" | |
private var token = "76543ab3244ac" | |
private var userID = 0 | |
private var addresses: [String] = [] | |
private var receivedData = NSMutableData() | |
private var getTokenTask: NSURLSessionTask! | |
private var getUserDataTask: NSURLSessionTask! | |
private var getAddressesDataTask: NSURLSessionTask! | |
override init(){ | |
super.init() | |
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() | |
session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil) | |
} | |
private var loginRequest: NSURLRequest{ | |
let lognRequestURL = NSURL(string: loginURL)! | |
let params = "username=admin&password=12345678" | |
let loginRequest = NSMutableURLRequest() | |
loginRequest.HTTPMethod = "POST" | |
loginRequest.HTTPBody = params.dataUsingEncoding(NSUTF8StringEncoding) | |
loginRequest.URL = lognRequestURL | |
return loginRequest | |
} | |
// start with this function | |
func getAddresses(){ | |
getTokenTask = session.dataTaskWithRequest(loginRequest) | |
getTokenTask.resume() | |
} | |
} | |
extension ExampleChainingRequest: NSURLSessionDataDelegate{ | |
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) { | |
// concat a piece of data | |
receivedData.appendData(data) | |
} | |
// handle everything inside delegate | |
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) { | |
if let error = error{ | |
return println(error) | |
} | |
if let result = NSString(data: receivedData, encoding: NSUTF8StringEncoding){ | |
println(result) | |
} | |
// do something with result | |
// 1. | |
if task == getTokenTask{ | |
println("we got token.") | |
let getCurrentUserURL = NSURL(string: getUserDataURL + "?token=\(token)")! | |
getUserDataTask = session.dataTaskWithURL(getCurrentUserURL) | |
getUserDataTask.resume() | |
// 2. | |
}else if task == getUserDataTask{ | |
println("we got userData.") | |
let getAddressesCurrentUserURL = NSURL(string: getAddressesURL + "?token=\(token)&userID=\(userID)")! | |
getAddressesDataTask = session.dataTaskWithURL(getAddressesCurrentUserURL) | |
getAddressesDataTask.resume() | |
// 3. | |
}else if task == getAddressesDataTask{ | |
println("we got user's addresses.") | |
println("finish") | |
} | |
// clear previous request data | |
receivedData = NSMutableData() | |
} | |
} | |
// start here | |
let getAddresses = ExampleChainingRequest() | |
getAddresses.getAddresses() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment