Test whether iOS notification is synchronous or asynchronous

Source: Internet
Author: User

The interview was asked this question, not very clear, write code test and record.

#pragma mark-Test notification
-(void) testnotification
{
Initialize a button
UIButton *button = [[UIButton alloc] initwithframe:cgrectmake (100, 100, 100, 50)];
Button.backgroundcolor = [Uicolor Orangecolor];
[Button settitle:@ "trigger notification" forstate:uicontrolstatenormal];
[Button addtarget:self action: @selector (Buttondown) forcontrolevents:uicontroleventtouchupinside];
[Self.view Addsubview:button];

Registration Notice
[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (actionnotification:)
Name:knotificationname
Object:nil];
}
-(void) Actionnotification: (nsnotification*) notification
{
nsstring* message = Notification.object;
NSLog (@ "message:%@", message);

Sleep (3); Sleep for 3 seconds.
NSLog (@ "Notify End of Speech");
}

-(void) Buttondown
{
[[Nsnotificationcenter Defaultcenter] postnotificationname:knotificationname object:@ "Notice to talk start"];
NSLog (@ "button to speak");
}

When you click the Trigger Notification button, the console prints as follows:

Through the time interval here, we can see that the notification process is synchronous.

After the notification is thrown, the viewer will not proceed until the notification event is completed (Here we hibernate for 3 seconds), that is, the process is synchronized by default, and when the notification is sent, the notification center waits for all observer to receive and process the notification before returning to poster;

Ps:

If you want to change synchronization to asynchronous, there is a way. For example, let the notification event handling method be executed in the child thread.

-(void) Actionnotification: (nsnotification*) notification
{
nsstring* message = Notification.object;
NSLog (@ "message:%@", message);
//
Sleep (3); Sleep for 3 seconds.
NSLog (@ "Notify End of Speech");
Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{

nsstring* message = Notification.object;
NSLog (@ "message:%@", message);

Sleep (3);

NSLog (@ "Notify End of speech:%@", [Nsthread CurrentThread]);

});
}

-(void) Buttondown
{
[[Nsnotificationcenter Defaultcenter] postnotificationname:knotificationname object:@ "Notice to talk start"];
NSLog (@ "button Talk:%@", [Nsthread CurrentThread]);
}

At this point, click the Trigger Notification button and the console prints the input:

Test whether iOS notification is synchronous or asynchronous

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.