iOS Multithreaded Development Basics

Source: Internet
Author: User

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

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.