Multi-threaded use of IOS

Source: Internet
Author: User
Tags gcd

Runloop

The Runloop object of the main thread is automatically opened, and UIApplication turns on a dead loop (message loop)

Each thread has its own Runloop object

Runloop 1.> Input Source Inputs resource: (Asynchronous messages, processing other threads) processing other threads back to the main thread to do things

Performselector:onthread (communication between threads)

2.> Timer Source Timer Resource:(synchronous message--processing the main thread) handles the main thread UI refresh, event handling, timer (there is no UI refresh, scrolling on the timed resolution interface)

Threading is particularly expensive in memory, so it requires precise management of threads

1. Check that the network request is placed in the main thread

2. See if the number of asynchronous requests is too large (number of child threads)

3. Is the amount of data too large? If it's too big, clear some unnecessary objects (invisible data, especially pictures)

Monitor memory warnings in a timely manner

-(void) Applicationdidreceivemesonry

Multithreading security how to control?

1. Refresh the access UI only in the main thread

2. If you want to prevent resource looting, you have to use synchronized for lock protection

3. If the asynchronous operation is to ensure thread safety, try to use GCD (some functions are thread-safe by default)

Many images are downloaded in an asynchronous thread, and if they fail, how do they handle them?

1. Re-download the image

2. After downloading, use the Runloop input source to return to the main thread refresh Uiimageview

Some pictures load relatively slow how to deal with?

1. Picture download put on async thread

2. Use placeholder images during image download

3. If the picture is too large, you can consider multi-threaded breakpoint download, set the request header information

Sdwebimage Specific implementation process

1. Download images using Nsoperationqueue and nsoperation

2. Also use some functions of gcd to decode GIF images

gif image (gif decomposition) ImageIO Remove all frames (each frame is a UIImage object) (This process is time consuming, put into an async thread)

Uiimageview * ImageView;

Imageview.animationimages =.;

3. Use URL as key, nsoperation as value

4. Use URL as key, UIImage as value

Is it possible to use nsnotificationcenter to transmit data and messages asynchronously or synchronously across multiple controllers?

1. If the notification is issued on the main thread, then the time-consuming action in the method that receives the notification is put into the asynchronous thread

[[Nsnotificationcenter Defaultcenter] Postnotificationname: @ "abc" object; nil];

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (TEST) Name: @ "abc" Object:nil];

-(void) Test {dispatch ...}

2. If the notification is emitted in an asynchronous thread, the method invoked after receiving the notification is executed by default in the asynchronous thread

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default,0), ^{

  [Nsnotificationcenter Defaultcenter] Postnotificationname: @ "abc" object; NIL];

});

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (TEST) Name: @ "abc" Object:nil];

-(void) test {}

Multi-threaded use of IOS

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.