The application of multithread in the actual project of iOS development

Source: Internet
Author: User

In the actual project development in order to give users a better experience, some delay operation we will put in the child thread.

Today we'll talk about the use of multithreading in practical projects.

Let's first look at the basics of multithreading:

1. The principle of Multithreading:

At the same time, the CPU can only handle one thread, that is, only one thread is working. The so-called multithreading concurrency (simultaneous) execution,

In fact, the CPU is fast in the scheduling between multithreading (toggle). If the CPU schedules a thread fast enough, it creates multithreading and

The illusion of execution.

2. In the actual project development is not the more threads the better, if a large number of threads, will consume a lot of CPU resources, the CPU will

Was exhausted, so the general mobile phone only open 1~3 thread is advisable, not more than 5.

3. Multithreading Advantages and Disadvantages:

Advantages: 1. Can improve the efficiency of the procedure

2. Can appropriately improve the utilization of resources, this utilization performance (CPU, memory utilization)

Disadvantages: 1. Opening a thread requires a certain amount of memory space (by default, the main thread occupies 1M,

Child threads occupy 512KB, if you open a large number of threads, will occupy a lot of memory space, reduce the program

's performance)

2. The more threads, the greater the overhead of the CPU on the scheduling thread

3. The more complex program design: such as the communication between threads, multithreading of data sharing, these

All require the processing of the program, increasing the complexity of the program.

4. Considerations for using threads in iOS development:

1. Do not put more time-consuming operations in the main thread

2. Time-consuming operation will be stuck to the main thread, seriously affecting the fluency of the UI, give users a "card" bad experience

Well, multithreading in the development of iOS conceptual things to say so much, let's simulate a development scenario:

We often encounter in development when you want to cache a group of pictures, but these pictures have to wait until you are buffered and then show on the UI,

But we are caching the image with the sdwebimage frame, the operation of the cache is asynchronous, how do we do so the cache is good

To do the next operation. Here is a simple and convenient way to achieve this:

I'll put the code first and explain it later:

1. Add a group let
        group = dispatch_group_create ()
        
        //cache picture for
        URL in picurls! {
            
            //2. Add the current download operation to the group
            Dispatch_group_enter (group)
            Sdwebimagemanager.sharedmanager (). Downloadimagewithurl (URL, options:SDWebImageOptions.init (rawvalue:0), Progress:nil, Completed: {(_, _, _, _, _) in
  
   //3. Leave the current group
                of Dispatch_group_leave (
                "in Cache ...")}
        }
        
        //To pass data to the caller via a closure (notify picture cache complete)
        dispatch_group_notify (Group, Dispatch_get_main_queue ()) {
            print (cache complete). ")
            finished ()
        }
  

Print the results as follows:


From the output we can see: we have done after the cache to perform the next operation.

is how to do it.

I've already marked it with numbers in my Code:

1. We first use

Let group = Dispatch_group_create ()
function to create a group that holds the buffered operation

2. Use this function to add each buffer operation to the group

Dispatch_group_enter (Group)
3. Cache picture I use the sdwebimage frame, we can see, I left the current group after buffering, use the following functions
Dispatch_group_leave (Group)
With these three steps, we can do what we want. Obviously not, doing these three systems inside will do something for us,

When we leave the current group, the system sends a notification that we will receive this notification when we receive this notification.

We can perform the finished operation, and the function to receive the notification is:

Dispatch_group_notify (Group, Dispatch_get_main_queue ()) {
            print (cache complete). ")
            finished ()
        }
The above is a very convenient way to achieve the functions we need



Related Article

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.