You can use the GCD function to delay the operation of the function as
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (Delayinseconds * nsec_per_sec)), Dispatch_get_main_queue (), ^{ });
now let's break down the parameters.
dispatch_time (dispatch_time_now, (int64_t) (Delayinseconds *nsec_per_sec )): nsec_per_sec The definition in the header file is as follows:
#define NSEC_PER_SEC1000000000ull/* nanoseconds PER Second * / This parameter represents how many nanoseconds are passed from now onwards dispatch_get_main_queue (): Indicates the home row^{}: Represents a block task. We can test how many nanoseconds are passed, and whether the task is scheduled to be executed asynchronously or synchronously by the home row, the code is as follows:
when time passes from now on how many nanoseconds dispatch_time_t when = Dispatch_time (Dispatch_time_now, (int64_t) (1.0 * nsec_per_sec)); void (^task) () = ^ { //delay operation executed code NSLOG (@ "%@", [Nsthread CurrentThread]); }; The number of nanoseconds passed by the home row Dispatch task asynchronously executes dispatch_after (when, Dispatch_get_main_queue (), task); The first execution is asynchronous, then the execution is synchronous NSLog (@ "come here");
The results of the implementation are as follows:
This shows that the dispatch task in the main queue is executed asynchronouslyinstead of changing the execution queue to a global queue and a serial queue, the result is exactly the same, so that the function performs an asynchronous operation.
there is a function in GCD to ensure that a piece of code is executed only 1 times during the program's operation! The function is as follows:
Static dispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{ })
The dispatch_once_t is defined in the header file as follows:
typedeflong dispatch_once_t; This type is a long type. the block code is executed when Oncetoken equals 0 o'clock. dispatch_once is thread-safe, as long as the thread safety involved will involve the lock,dispatch_once inside also has a lock, performance than mutex lock high! with this function we can write a singleton pattern.The Singleton mode can guarantee the process of running a program, a class has only one instance and the instance is easy for outside access, thus it is convenient to control the number of instances, and save system resources, when the application needs to share a resource can be implemented by a singleton mode. Single-instance mode arc and MRC two cases, we can use the macro to determine whether the ARC environment
#if __has_feature (OBJC_ARC)//arc#else//Mrc#endif
Simple singleton mode in the ARC environment:
@implementation soundtools//defines a static member that holds a unique instance of static ID instance;//guarantees that the object is only allocated once in memory space, through Dispatch_ Once can guarantee that the allocation and initialization of a single case is thread-safe + (Instancetype) Allocwithzone: (struct _nszone *) zone { static dispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{ instance = [Super Allocwithzone:zone]; }); return instance;} Ensure that the object is initialized only once + (Instancetype) sharedsoundtools { static dispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{ instance = [[Self alloc] init]; }); return instance;} -(ID) Copywithzone: (Nszone *) Zone { return instance;} The @end test code is as follows:-(void) viewdidload { [super viewdidload]; Soundtools *s1 = [Soundtools sharedsoundtools]; NSLog (@ "%p", S1);} -(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event { Soundtools *s2 = [Soundtools sharedsoundtools]; NSLog (@ "%p", S2);}
two ways to print out the exact same address!
in the MRC environment, the following code is available:
Defines a static member that holds a unique instance of static ID instance;//to ensure that the object is allocated only once in memory space, and that by dispatch_once the allocation and initialization of the singleton is thread-safe + (Instancetype) Allocwithzone: (struct _nszone *) zone {static dispatch_once_t oncetoken; Dispatch_once (&oncetoken, ^{instance = [Super Allocwithzone:zone]; }); return instance;} Ensure that the object is initialized only once + (Instancetype) sharedsoundtools {static dispatch_once_t oncetoken; Dispatch_once (&oncetoken, ^{instance = [[Self alloc] init]; }); return instance;} -(ID) Copywithzone: (Nszone *) zone {return instance;} #pragma MARK-MRC memory Management method/** because the Singleton object is saved in the static zone, you need to override the memory management method and cancel the default reference count operation! *///default will reference count-(OneWay void) Release {//Do nothing, like highlight}//default reference count +1, return an Object-(Instancetype) retain {return in Stance;} The default is to add the automatic release tag, delay release! -(Instancetype) Autorelease {return instance;} Returns the number of values that the object references to the current object-(Nsuinteger) Retaincount {///Source: Limits.h Adjusts the length of the integer according to the CPU's schema return Ulong_max;}
iOS multithreaded operation (vii)--GCD deferred operation with one-time code