Best practices for IOS---two network requests

Source: Internet
Author: User

When loading a system using a URL, the primary two methods can perform HTTP requests and receive responses:

I. Best practices for synchronizing requests

  • Only use the synchronization request in the background thread, unless you are sure that the requested access is local file resources, otherwise do not use the main thread;
  • Use a synchronization request only if you know that the returned data does not exceed the applied memory. Remember that the entire response body is in the memory of the code. If the response is large, it can cause a memory overflow problem with the app. In addition, contemporary? When parsing a response to the desired format, it may be necessary to replicate the returned data, which can cause memory to increase by one-fold;
  • The validation error and the HTTP response status code returned by the call before processing the returned data;
  • If the source URL requires validation, do not use a synchronization request because the synchronization framework does not support the response to the authentication request;
  • If you need to provide a progress bar to the user, then do not use the synchronization request because the request is atomic and cannot provide intermediate progress indication information;
  • If you need to parse the response data incrementally through a stream parser, do not use synchronous requests;
  • If you need to cancel before the request is complete, do not use a synchronization request.

Ii. Best Practices for asynchronous requests

  • For large uploads or downloads, use asynchronous requests to reduce the memory footprint of your application;
  • Use asynchronous requests in cases where authentication is required;
  • If you need to provide progress feedback to the user, then use an asynchronous request;
  • Be careful when using asynchronous requests on background threads, provide a running loop;
  • For simple requests that can be easily dispatched and completed in the request queue of a background thread, the use of asynchronous requests is somewhat overkill;
  • If you are using an input stream to upload data, implement the Connection:newbodystream: method to avoid replication of the inflow

Best practices for IOS---two network requests

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.