Encapsulate the logic from dispatch_async to the main thread. The dispatch_async thread

Source: Internet
Author: User

Encapsulate the logic from dispatch_async to the main thread. The dispatch_async thread

Background: Sometimes the content to be executed in the Code is placed in the main thread for execution. However, if the Code already in the main thread calls dispatch_async, crash may occasionally occur, therefore, you need to determine whether it is already in the main thread. The common practice is similar to the following code:

    ......    if ([NSThread isMainThread]) {        block();    } else {        dispatch_async(dispatch_get_main_queue(), block);    }    ......

Therefore, an encapsulation is performed in the SDWebImage library, and the content is placed in a macro:
#define dispatch_main_async_safe(block)\    if ([NSThread isMainThread]) {\        block();\    } else {\        dispatch_async(dispatch_get_main_queue(), block);\    }

In this way, you can call the dispatch_main_async_safe safe dispatching task in the code to run it in the main thread.

However, the problem is that the block in the macro cannot be used for breakpoint debugging, for example:
dispatch_main_async_safe(^{    if(a) {        [self foo1];    }    else {        [self foo2];    }});

We cannot add a breakpoint to the if judgment to debug step by step.

Therefore, I had the idea of encapsulating a function for calling.

We can find that methods such as dispatch_async are actually C interfaces, so there are a lot of problems in the encapsulation process.

Finally, the user warned that OC can directly call the C method without changing the file. mm suffix, but if you call the C ++ method, you need to change the file. mm suffix.

My code contains many. m Files and many. mm files, so I had to encapsulate the C and C ++ interfaces for use.


The following is the C interface:
Header file
/// XXXDispatchMainQueueSafeC. h /// Created by robyzhou on 15/7/16. // Copyright (c) 2015 robyzhou. all rights reserved. // # ifndef _ XXX _ XXXDispatchMainQueueSafeM __# define _ XXX _ XXXDispatchMainQueueSafeM __# include <stdio. h> typedef void (^ Cblock) (); void dispatch_main_async_XXX_m (Cblock block); void dispatch_main_sync_XXX_m (Cblock block); # endif


M file

/// XXXDispatchMainQueueSafeC. c // XXX // Created by robyzhou on 15/7/16. // Copyright (c) 2015 robyzhou. all rights reserved. // # include "XXXDispatchMainQueueSafeM. h "void dispatch_main_async_XXX_m (Cblock block) {if ([NSThread isMainThread]) {block ();} else {dispatch_async (dispatch_get_main_queue (), block );}} void dispatch_main_sync_XXX_m (Cblock block) {if ([NSThread isMainThread]) {block ();} else {dispatch_sync (dispatch_get_main_queue (), block );}}

The following are the C ++ interfaces:
Header file

/// XXXDispatchMainQueueSafe. h // XXX // Created by robyzhou on 15/7/15. // Copyright (c) 2015 robyzhou. all rights reserved. // # import <Foundation/Foundation. h> @ interface XXXDispatchMainQueueSafe: NSObjecttypedef void (^ block) (); void forward (block); void dispatch_main_sync_XXX_mm (block); @ end

Mm File

/// XXXDispatchMainQueueSafe. m // XXX // Created by robyzhou on 15/7/15. // Copyright (c) 2015 robyzhou. all rights reserved. // # import "XXXDispatchMainQueueSafeMM. h "@ implementation implements callback (block) {if ([NSThread isMainThread]) {block ();} else {dispatch_async (dispatch_get_main_queue (), block );}} void dispatch_main_sync_XXX_mm (block) {if ([NSThread isMainThread]) {block ();} else {dispatch_sync (dispatch_get_main_queue (), block);} @ end

In this way, if you are calling in the. m file, you can directly call tranquility/dispatch_main_sync_XXX_m. If you are calling in the. mm file, you can directly call dispatch_main_async_XXX_mm/dispatch_main_sync. For example:

dispatch_main_async_XXX_m(^{    if(a) {        [self foo1];    }    else {        [self foo2];    }});

You can add breakpoints to the block for debugging.

If you have any questions, please leave a message and discuss them together. Thank you!

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.