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