Asynchronous Notification Failed
Asynchronous Notification Failed
- Subject: Asynchronous Notification Failed
- From: Bing Li <email@hidden>
- Date: Tue, 17 May 2011 05:19:06 +0800
Dear all,
I am learning how to program an asynchronous notification system. But it
only works as a synchronous one. I was told I should implement a RunLoop in
the code. How to do that? I am not familiar with RunLoop. The sample code is
listed as follows. Could you please help me how to solve the problem?
Thanks so much!
Bing
The code, WorkingApp, did something and invoked an asynchronous notification
after the job was done.
....
@implementation WorkingApp
- (void) Print
{
NSLog(@"I am doing a tough job!");
// The commented line works. But it gets a synchronous notification.
// [[[NSNotificationQueue alloc]
initWithNotificationCenter:[NSNotificationCenter defaultCenter]]
enqueueNotification: [NSNotification notificationWithName:@"Done"
object:self] postingStyle:NSPostNow];
// The notification does NOT work.
[[[NSNotificationQueue alloc]
initWithNotificationCenter:[NSNotificationCenter defaultCenter]]
enqueueNotification: [NSNotification notificationWithName:@"Done"
object:self] postingStyle:NSPostWhenIdle];
NSLog(@"I need to leave!");
}
....
A delegate, MonitorDelegate, is implemented as follows.
....
@class Monitor;
@protocol MonitorDelegate
@optional
- (void) IHaveDone: (Monitor *) app;
@end
....
An object, Monitor, is implemented. Through it, other code can be notified
asynchronously.
@implementation Monitor
@synthesize delegate;
- (void) IHaveDone: (NSNotification *) notification
{
NSLog(@"OK");
if ([self.delegate respondsToSelector:@selector(IHaveDone:)])
{
[self.delegate IHaveDone:self];
}
}
- (void) setUpNotification: (NSString *) notification withSelector:
(SEL) methodName
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:methodName name:notification object:nil];
}
- (void) registerNotifications
{
[self setUpNotification:@"Done" withSelector:@selector(IHaveDone:)];
}
- (id) init
{
if (self = [super init])
{
[self registerNotifications];
}
return self;
}
@end
An application, AppController, which is interested in the asynchronous
notification, is as follows. I put sleep(5) deliberately to test if the
asynchronous notification works.
@implementation AppController
@synthesize monitor;
- (void) IHaveDone:(Monitor *)app
{
NSLog(@"I know you have done!");
sleep(5);
NSLog(@"You are a good guy!");
}
- (void) sayHi
{
NSLog(@"Hi");
}
- (id) init
{
self.monitor = [[Monitor alloc] init];
self.monitor.delegate = self;
[self.monitor.delegate sayHi];
return self;
}
@end
A main code is implemented to start the test. I put a console input into the
code to avoid the asynchronous notification being terminated.
....
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
WorkingApp *app = [[WorkingApp alloc] init];
AppController *controller = [[AppController alloc] init];
[app Print];
char s[100];
scanf("%s", s);
[app release];
[controller release];
[pool drain];
return 0;
....
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden