开发者

NSThread - get bool value

I am trying to get the boolean value that's returned by -(BOOL)backupDropletUpdateAvailable through NSThread.

To do this, I've tried the following:

` BOOL isAvailable = NO;

[NSThread detachNewThreadSelector:@selector(backupDropletUpdateAvailable) toTarget:isAvailable withObject:nil];

if (isAvailable == YES)
{//etc

W开发者_高级运维hich returns a warning since BOOL is an integer and toTarget: is a pointer. But how can I get the value? If I don't do this on a separate thread, my xib will lag when it appears.

Thanks :)


The method run by the thread needs to write to a location that the objects that care about the result would have access to. One solution would be to have a method wrap the call, get the result, and post a notification that includes the result in the user info. Objects that care can then handle the notification. Note that the objects must be created before the thread is started, otherwise the object might miss the notification.

A sketch of the solution is:

#define kDropletAvailabilityNotificationName @"com.myapp.notifications.DropletAvailability"

@implementation MyObject
- (void)registerNotifications {
    [[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(dropletAvailabilityNotification:)
     name:kDropletAvailaibiltyNotificationName
     object:nil];
}

- (void)unregisterNotifications {
    [[NSNotificationCenter defaultCenter]
     removeObserver:self];
}

- (void)dropletAvailabilityNotification:(NSNotification *)note {
    NSNumber *boolNum = [note object];
    BOOL isAvailable = [boolNum boolValue];
    /* do something with isAvailable */
}

- (id)init {
    /* set up… */
    [self registerNotifications];
    return self;
}

- (void)dealloc {
    [self unregisterNotifications];
    /* tear down… */
    [super dealloc];
}
@end

@implementation CheckerObject
- (rsotine)arositen {
    /* MyObject must be created before now! */
    [self performSelectorInBackground:@selector(checkDropletAvailability) withObject:nil];
}

- (void)checkDropletAvailability {
    id pool = [[NSAutoreleasePool alloc] init];
    BOOL isAvailable = [self backupDropletUpdateAvailable];
    NSNumber *boolNum = [NSNumber numberWithBool:isAvailable];
    [[NSNotificationCenter defaultCenter]
     postNotificationName:kDropletAvailaibiltyNotificationName
     object:boolNum];
    [pool drain];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜