Multiple request with AFNetworking

左心房为你撑大大i 提交于 2019-12-21 02:33:10

问题


I'm trying to do multiple request in background to download many jsons and check data from them but I don't know how to use AFNetworking in that case.

I tried to do like Wiki explaings but when it's going to download the second file then the app breaks. I want to do all the process in background.

Thanks


回答1:


AFNetworking will definitely handle this. We use it for exchanging data with a RESTful set of services. The things to keep in mind:

  • An operation (eg. AFHTTPRequestOperation) can only be used once.
  • An operation is asynchronous.
  • Put your operations in an NSOperationQueue, or use AFHTTPClient (suggested) to manage the operations for you.
  • When sending multiple requests, always assume that the responses will come back in a random sequence. There is no guarantee that you will get the responses in the same sequence as the requests.

Hope this helps to point you towards a solution to your problem. Without more detail in your question, it's difficult to give you a specific answer.




回答2:


Check out AFHTTPClient's enqueueBatchOfHTTPRequestOperations:progressBlock:completionBlock:, which lets you enqueue multiple requests operations at once with the added bonus of having a completion handler that is called when all of those requests have finished, as well as a block for tracking the progress. Also note, that every single operation can still have its own completion handler (useful if you have to process the results of a request, for example).

If you don't need to customize the request operation (and don't need individual completion blocks), you can also use enqueueBatchOfHTTPRequestOperationsWithRequests:progressBlock:completionBlock:, which allows you to pass an array of NSURLRequest directly without having to build the operations yourself.



来源:https://stackoverflow.com/questions/12830666/multiple-request-with-afnetworking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!