Application of multi-threading in iOS development in practical projects

Source: Internet
Author: User

In the actual project development in order to be able to give the user a better experience, some delay operation we will be placed in the sub-thread.

Today we are going to 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 process one thread, that is, only one thread is working. So-called multithreading concurrency (simultaneous) execution,

In fact, the CPU is quickly dispatched between multiple threads (switching). If the CPU schedules the thread fast enough, it causes 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

Be exhausted, so the general mobile phone only open a thread is appropriate, no more than 5.

3. Advantages and disadvantages of multithreading:

Advantages: 1. Can improve the execution efficiency of the procedure appropriately

2. Can appropriately improve the utilization of resources, which is shown in (CPU, memory utilization)

Cons: 1. Open threads require a certain amount of memory space (by default, the main thread takes up 1M,

A child thread consumes 512KB, and if a large number of threads are turned on, it consumes a lot of memory space and reduces the program

's performance)

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

3. The more complex the programming, such as the communication between threads, multi-threaded data sharing, these

All require program processing, which increases the complexity of the program.

4. Considerations for using threading in iOS development:

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

2. Time-consuming operation will be stuck in the main thread, seriously affect the smoothness of the UI, give users a "card" bad experience

Well, multithreading in iOS is about developing conceptual stuff so much, let's simulate a scenario in development:

We often encounter in development, when you want to cache a set of pictures, but these pictures must wait until you buffer well before you show up on the UI,

But when we cache the picture, we use the Sdwebimage framework, the cached operation is asynchronous, how do we do the cache?

What about the next step? Here is a very simple and convenient way to implement:

Let me put the code in the first place and explain it later:

1. Add a group of 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                Dispatch_group_leave (group)                print ("in cache ...")}            )        }                //pass data to the caller via a closure (notifies the picture that the cache is complete)        dispatch_group_notify (Group, Dispatch_get_main_queue ()) {            print ("Cache complete! ")            finished ()        }
   

From the output we can see: We do the cache after the completion of the subsequent operation.

How did that happen?

I've already marked the numbers in the code:

1. We first use

Let group = Dispatch_group_create ()

function to create a group to hold the buffered operation

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

Dispatch_group_enter (Group)

3. Cached images I'm using the sdwebimage framework, and we can see that I left the current group after the buffer was completed, using the following function

Dispatch_group_leave (Group)

Can you do what we want with these three steps? Obviously not, doing the three of these systems will do something for us.

When we leave the current group, the system will send a notification that we can 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 implement the functions we need

52679473

Application of multi-threading in iOS development in practical projects

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.