Cancellation of an issued network request based on Afnetworking 3.0

Source: Internet
Author: User

In general, there is not much demand for unsolicited cancellation requests

Unless the following conditions

1. For example, e-commerce applications are frequently requested and the data volume is large

2. High performance requirements

3. Network environment is relatively poor

When a user opens an interface and sees a long waiting box, the user is likely to exit the current interface to explore other interfaces. In the above cases, we need to do the control of network resources. When a request is sent, there is no need to wait for his results when we should take the initiative to cancel the request.

Proactive cancellation of requests not only saves network resources, but also avoids delayed memory release issues caused by block reference VCs. Many network frameworks now support this operation, as long as you get the request queue ready to initiate/cancel requests. In order to operate the queue we will be in the VC holding queue, the design angle of each need to request the VC has a number of queues, and the manual cancellation request is particularly troublesome.

The main ideas I've designed here are:

The request queue is managed dynamically by category to avoid the VC directly holding the request queue.

The cancel request operation is triggered by the runtime.

Here is the code

Afhttpsessionmanager *session = [Afhttpsessionmanager manager];

for (0; I < 10; i++) {nsurlsessiondatatask *datatask = [Session GET: @ "Https://api.github.com/users/facebook" parameters:@[] progress:^ ( nsprogress * _nonnull downloadprogress) {} success:^ (nsurlsessiondatatask * _nonnull task, id _ Nullable responseobject) {[self Removetask:task];} failure:^ ( nsurlsessiondatatask * _nullable task, NSError * _Nonnull Error) {[self Removetask:task];}]; [self Addtask:datatask];}          

Here is the category method


Paste_image.png


Let's look at the code snippet for canceling the request

I'm going to use runtime and another VC to disappear. Trigger Cancel Request operation


Paste_image.png


Category method Cancel Request


Paste_image.png
Paste_image.png

Open source for Operation runtime

Import "Aspects.h"

It's just a way of thinking dictionary access without doing thread safety control can be improved in many places

If not every VC has this demand, can be maintained by the specific VC list to avoid all the VC to make judgments.

Now it seems that each VC does not do extra work just add delete queue can complete the interface disappears when the request is automatically canceled.

============== Promotion =================

I have a story, do you have any wine?

I have wine, and only wine, because I am poor only wine.

North Canton has a story, there are dreams, love, there are gossip, there is life.

Come [North Canton] and listen to their stories.

Download iOS version:http://7x2x43.com1.z0.glb.clouddn.com/bsgwbtt.html

or AppStore search North Canton

[North Canton]-dedicated to the realization of the dream of you

Cancellation of an issued network request based on Afnetworking 3.0

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.