The way iOS executes only once

Source: Internet
Author: User

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

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.