How to use Asynchronous Network in iPhone threads

Source: Internet
Author: User

From: http://kensou.me/blog? P = 233

In my personal experience, the experience of using asynchronous nsurlconnection in iPhone threads is totally different from pleasure. He brought me a lot of trouble. For example, a problem occurred a few days ago when the customer located the problem.

The process is as follows: the customer feedback fails to normally use a network-related function provided by us, and the network callback is not received. However, other callbacks can work normally, and all Callbacks are put in the same logic somewhere.

I first confirmed whether his usage is correct, confirmed the correctness of the input parameters, and verified the correct callback settings and the use of the callback function. Everything is fine, but the callback cannot be received.

Everything is amazing. There is nothing special about function calling, but this function alone is not working properly. After some confirmation, we found that the data did not arrive at the server. However, it is normal to use the same method in our own environment.

It took nearly five hours, but I still did not find the cause. When I was about to give up, I suddenly thought that his call location was called by the thread? After confirmation, it was found that it was called in the thread! They finally solved the problem by calling mongomonmainthread.

I used to call an Asynchronous Network in a thread. I asked my colleagues and colleagues to inform me that the Asynchronous Network requires its own runloop. Therefore, to use an Asynchronous Network in the thread, you must have your own runloop, it can be placed in the runloop of the main thread.

However, in this way, the performance of Multithreading is inevitably reduced, because network work is completed in the main thread. At the same time, I suspect that the network performance design is not very low!

Just a little time, I carefully reviewed Apple's development documentation and found that all nsthread has its own nsunloop, the nsurlconnection callback will be called back to the current thread!

This is totally different from what I have encountered! I cannot receive a callback in the current thread!

After careful searching on the network, we found that the original reason was that the thread's nsunloop was invalid during network callback.

Network calls in our common threads are as follows:

- (void)threadFunc
{
NSAutoReleasePool *pool = [NSAutoReleasePool new];
//do something
……

// Send your request
[Nsurlconnection connectionwithrequest: XXX];

//do some other thing
……
[pool release];
}

In general, this function will soon finish, and the thread will end. So when the network returns, your thread is finished. Your network callback depends on the thread's nsunloop, but the thread has ended, so your network callback cannot be received !! Likewise, all APIs such as Alibaba mafterdelay and nstimer are not working properly!

So how can we make him work properly? It is very simple. Before you finish the task, let the thread not end, just keep it empty.

- (void)threadFunc
{
NSAutoReleasePool *pool = [NSAutoReleasePool new];
//do something
……

// Send your request
[Nsurlconnection connectionwithrequest: XXX];

// Do some other thing
......

/**

At this time, the thread does not end, waiting for the network data to return

*/

while(shouldExit)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
[pool release];
}

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.