I am trying to update a structure with a multi-level nested async callback, as each level callback provides information for the next batch of requests until everything is completed. It looks like a tree structure. And every time I can go only one level below.
However, the first attempt with the inout parameter failed. Now I find out the reason, thanks to the great answers here: Inout parameter in async callback not working properly
My searches have yet to be resolved. The only way I can think of is to save the value in a local file or persistent storage and change it every time. And after writing the sample code, I think the global var can help me with this. But I think the best way is to create an instance of the structure for this work. And for each round of requests, I store the information for this round in one place, in order to avoid the mess created by different rounds working at the same time.
With the sample code below, only the global update works. And I believe that the reason the other two are unsuccessful coincides with the question that I mentioned above.
func testThis() { var d = Data() d.getData() } let uriBase = "https://hacker-news.firebaseio.com/v0/" let u: [String] = ["bane", "LiweiZ", "rdtsc", "ssivark", "sparkzilla", "Wogef"] var successfulRequestCounter = 0 struct A {} struct Data { var dataOkRequestCounter = 0 var dataArray = [A]() mutating func getData() { for s in u { let p = uriBase + "user/" + s + ".json" getAnApiData(p) } } mutating func getAnApiData(path: String) { var req = NSURLRequest(URL: NSURL(string: path)!) var config = NSURLSessionConfiguration.ephemeralSessionConfiguration() var session = NSURLSession(configuration: config) println("p: \(path)") var task = session.dataTaskWithRequest(req) { (data: NSData!, res: NSURLResponse!, err: NSError!) in if let e = err { // Handle error } else if let d = data { // Successfully got data. Based on this data, I need to further get more data by sending requests accordingly. self.handleSuccessfulResponse() } } task.resume() } mutating func handleSuccessfulResponse() { println("successfulRequestCounter before: \(successfulRequestCounter)") successfulRequestCounter++ println("successfulRequestCounter after: \(successfulRequestCounter)") println("dataOkRequestCounter before: \(dataOkRequestCounter)") dataOkRequestCounter++ println("dataOkRequestCounter after: \(dataOkRequestCounter)") println("dataArray count before: \(dataArray.count)") dataArray.append(A()) println("dataArray count after: \(dataArray.count)") if successfulRequestCounter == 6 { println("Proceeded") getData() } } } func getAllApiData() { for s in u { let p = uriBase + "user/" + s + ".json" getOneApiData(p) } }
Well, in my actual project, I successfully add var to the structure in the first batch of callbacks, and in the second it failed. But I could not get it to work in the sample code. I tried many times, so it took me so long to update my question with a sample code. In any case, I think that the main problem is to study the appropriate approach for this task. So I just put it off for a while.
I think there is no way to do this with closure, given how closure works. But still you want to ask and find out the best way.
Thanks.
swift
LiweiZ
source share