我试图通过NSThread获取-(BOOL)backupDropletUpdateAvailable
返回的布尔值。
为此,我尝试了以下内容:
`BOOL isAvailable = NO;
[NSThread detachNewThreadSelector:@selector(backupDropletUpdateAvailable) toTarget:isAvailable withObject:nil];
if (isAvailable == YES)
{//etc
由于BOOL是整数而toTarget:
是指针,因此返回警告。但是我怎样才能获得价值呢?如果我不在单独的线程上执行此操作,则我的xib在出现时会滞后。
谢谢:)
答案 0 :(得分:1)
线程运行的方法需要写入关心结果的对象可以访问的位置。一种解决方案是让方法包装调用,获取结果,并在用户信息中发布包含结果的通知。然后,关心的对象可以处理通知。请注意,必须在启动线程之前创建对象,否则对象可能会错过通知。
解决方案的草图是:
#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];
}