Multithreaded programming: When downloading data, it opens up sub-threads, reduces blocking time, and runs concurrently with the main thread, improving the user experience
1.Thread
1> new Thread object, with a selector method, call the Start method, turn on the child thread
2>thread to request data from the Selector method that is initialized
3> data please come back, call the self of the Performselectoronmainthread method, with a selector, and the corresponding parameters, usually the data requested back, if not self, will not find the Selector method// Must be self
4> processing of the requested data to implement UI updates
2.gcd-great Central Dispatch
1> new queue using dispatchqueue type
2>dispatch_queue.async{
Request Data Code
Cut back to the main thread
dispatchqueue.main.async{
Updating UI Code
}
}
3.Operation
1> new queue using operationqueue type
2> uses the Blockoperation type to create a new operation, the operation first requests the data, in the call self of the Performselectoronmainthread method to cut back the main thread, and finally update the UI
Code
Let operation = blockoperation {
Let url = URL (string: "Http://pic37.nipic.com/20131227/17514635_163144299116_2.jpg")
Let data = NSData (contentsof:url!) as! Data
Let img = UIImage (data:data)
To update the UI with the specified method, the caller must be the current viewcontroller!!!
Self.performselector (onmainthread: #selector (self.updateimage (Image:)), with:img, Waituntildone:false)
}
Queue.addoperation (Operation)
3> adding operations to the queue
iOS Multithreaded Development Basics