iOS Development (64) The GCD task executes at most once
1 Preface
Use the Dispatch_once function for the lifetime of the APP to ensure that you want to make sure that each piece of code executes only once, even if it is called multiple times in different parts of the code (such as singleton initialization).
2 code Examples
Zyappdelegate.m
[Plain]
/An identity for scheduling a function once
Static dispatch_once_t Oncetoken;
Block Object
void (^executedonlyonce) (void) = ^{
static Nsuinteger numberofentries = 0;
numberofentries++;
NSLog (@ "Executed%lu time (s)", (unsigned long) numberofentries);
};
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Declaring a queue
dispatch_queue_t concurrentqueue = dispatch_get_global_queue (dispatch_queue_priority_default, 0);
Queue to execute once
Dispatch_once (&oncetoken, ^{dispatch_async (Concurrentqueue,
Executedonlyonce);
});
Dispatch_once (&oncetoken, ^{dispatch_async (Concurrentqueue,
Executedonlyonce);
});
Self.window = [[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];
Override point for customization after application launch.
Self.viewcontroller = [[[Zyviewcontroller alloc] initwithnibname:@ "Zyviewcontroller" Bundle:nil] autorelease];
Self.window.rootViewController = Self.viewcontroller;
[Self.window makekeyandvisible];
return YES;
}
An identity for scheduling the function once
Static dispatch_once_t Oncetoken;
Block Object
void (^executedonlyonce) (void) = ^{
static Nsuinteger numberofentries = 0;
numberofentries++;
NSLog (@ "Executed%lu time (s)", (unsigned long) numberofentries);
};
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Declaring a queue
dispatch_queue_t concurrentqueue = dispatch_get_global_queue (dispatch_queue_priority_default, 0);
Queue to execute once
Dispatch_once (&oncetoken, ^{dispatch_async (Concurrentqueue,
Executedonlyonce);
});
Dispatch_once (&oncetoken, ^{dispatch_async (Concurrentqueue,
Executedonlyonce);
});
Self.window = [[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];
Override point for customization after application launch.
Self.viewcontroller = [[[Zyviewcontroller alloc] initwithnibname:@ "Zyviewcontroller" Bundle:nil] autorelease];
Self.window.rootViewController = Self.viewcontroller;
[Self.window makekeyandvisible];
return YES;
}
Zyviewcontroller.m
[Plain]
-(void) viewdidload
{
[Super Viewdidload];
Zymysingleton *test = [[Zymysingleton alloc] init];
Cyclic Single Example method
for (int i=0; i<5; i++) {
[Test sharedinstance];
}
[Test release];
}
-(void) viewdidload
{
[Super Viewdidload];
Zymysingleton *test = [[Zymysingleton alloc] init];
Cyclic Single Example method
for (int i=0; i<5; i++) {
[Test sharedinstance];
}
[Test release];
}
Zymysingleton.m
[Plain] View plaincopyprint?-(ID) sharedinstance{
static Zymysingleton *sharedinstance = nil;
An identity for scheduling the function once
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Sharedinstance = [Zymysingleton new];
NSLog (@ "Sharedinstance is ======>%@", sharedinstance);
});
return sharedinstance;
}
-(ID) sharedinstance{
static Zymysingleton *sharedinstance = nil;
An identity for scheduling the function once
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Sharedinstance = [Zymysingleton new];
NSLog (@ "Sharedinstance is ======>%@", sharedinstance);
});
return sharedinstance;
}
Display results after running the console
The way iOS executes only once