views:

26

answers:

1

Hello,

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

Which 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 :)

+1  A: 

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];
}
Jeremy W. Sherman
thanks! this solved my problem.
David Schiefer