NSThread - 获取布尔值

发布于 2024-09-29 13:37:47 字数 429 浏览 9 评论 0原文

我试图通过 NSThread 获取 -(BOOL)backupDropletUpdateAvailable 返回的布尔值。

为此,我尝试了以下操作:

` BOOL isAvailable = NO;

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

if (isAvailable == YES)
{//etc

它会返回一个警告,因为 BOOL 是一个整数,而 toTarget: 是一个指针。但我怎样才能得到这个值呢?如果我不在单独的线程上执行此操作,我的 xib 在出现时将会滞后。

谢谢 :)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2024-10-06 13:37:47

线程运行的方法需要写入关心结果的对象可以访问的位置。一种解决方案是使用一个方法包装调用、获取结果并发布一条通知,其中将结果包含在用户信息中。然后关心的对象可以处理该通知。请注意,对象必须在线程启动之前创建,否则对象可能会错过通知。

解决方案的草图是:

#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];
}

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文