简体   繁体   中英

iOS network operation when app become active

In my current iOS app, I need to perform several network operation when the app becomes active (I'd rather get everything at once than to query things when user click on dedicated tab). I'm using a UIViewController that should be displayed when the app goes to the foreground and handle all network stuff in it.
Is that the recommended way to do it ?

The requests I need to issue are something like:

request1
   if result1
      request2
         request3
   else if result2
      request4
         request3

In order to avoid spaghetti code, is there a lib to use that enable chained requests like those ones? I'm using ASIHttpRequest and I did not find any nice way to perform that (even with queue).

Nothing wrong with that, assuming your UIViewController is some sort of "loading". Perhaps a better way to handle it would be to create a NSObject subclass, and have that handle your network operations, so in the ApplicationDidBecomeActive: you can just fire that off, and still have the sure carrying on with your app.

AFNetworking 0.9 added the ability to batch requests. AFHTTPCLient -enqueueBatchOfHTTPRequestOperationsWithRequests:progressBlock:completionBlock: enqueues a set of operations; when each operation finishes it fires its own completionBlock like normal, as well as the progress block, which tells you how many of the requests have finished. Then, once all are finished, the final completion block executes.

So in your case, it could be as simple as:

[someClient enqueueBatchOfHTTPRequestOperationsWithRequests:[NSArray arrayWithObjects:request1, request2, request3, request4, nil] progressBlock:nil completionBlock:(controller logic here)];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM