Good pattern for Internet requests with Grand Central Dispatch?

前端 未结 4 1234
醉梦人生
醉梦人生 2021-02-04 11:54

I\'m currently using synchronous ASIHTTPRequest with GCD queues to download data from the Internet, then parse the response data with JSONKit. What do you think about this patte

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-04 12:09

    That code looks ok for a single network connection, but if you are using ASIHTTPRequest you will likely be a mobile application. For multiple concurrent downloads I would implement a queue (see "Using a queue" in the ASIHTTPRequest docs) where you can specify the amount of maximum simultaneous connections (say use 2 on GPRS and 8 on wifi) or throttle the bandwidth. Then, in the finish selector use GDC or something else to run the processing of the data out of the main UI thread.

    In essence, using blocks with ASIHTTPRequest for the simple case only brings you a different syntax with regards to NSURLConnection, as fakeAccount22 mentioned. And NSURLConnection also has synchronous methods, so you can avoid an external dependency (and potential source of bugs/problems) and use that in blocks.

提交回复
热议问题