SDWebImage of RunLoop

In the start of the function SDWebImage in SDWebImageDownloaderOperation, call the CFRunLoopRun (), we look at CFRunLoopRun in the end is what to do, what played a role.

Each thread has a runloop, can neither create nor destroy runloop thread. Core Foundation created for you on demand, through CFRunLoopGetMainavailable runloop the current thread. Call CFRunLoopRun can runloop current thread running in the default mode until the call CFRunLoopStopto stop runloop. You can also call CFRunLoopRunInModeto make runloop current thread running up to a specified period of time or until the mode runloop is stopped. [Runloop only a case of running at least a source or a timer may be monitored in the request mode. ]

Generally the main thread will automatically run runloop, we generally do not want to control. In other sub-thread, if required we need to manage. After using runloop, you can imagine the thread into a loop; if not this cycle, the child thread to complete the task, this thread is over. So if you need a thread handles a variety of events and not let it end, we need to run runloop.

In SDWebImageDownloaderOperation in

. 1 
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10 < large column  SDWebImage of runloop / span>
. 11
- (void)start{
...
self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO];
...

if(self.connection){
...
CFRunLoopRun()
...
}
}
1
2
3
4
5
- (void)cancelInternalAndStop {
if (self.isFinished) return;
[self cancelInternal];
CFRunLoopStop(CFRunLoopGetCurrent());
}

After you create self.connection successful implementation of the CFRunLoopRun (), opened runloop. When finished, or failed to stop the calls CFRunLoopStop runloop. If you do not open runloop then, after executing start () after the task is complete, NSURLConnection agency would not be executed. runloop cycle is equivalent to sub-thread can be flexibly control the life cycle of the child thread.

Guess you like

Origin www.cnblogs.com/sanxiandoupi/p/11713119.html