Objective c iOS-如何在线程(使用GCD)结束时收到通知';他的工作

Objective c iOS-如何在线程(使用GCD)结束时收到通知';他的工作,objective-c,multithreading,nsthread,grand-central-dispatch,Objective C,Multithreading,Nsthread,Grand Central Dispatch,我开始使用GCD,我需要知道某个线程何时结束了它的工作 我的代码: dispatch_queue_t registerDeviceQueue = dispatch_queue_create("RegisterDevice", NULL); dispatch_async(registerDeviceQueue, ^{ [self registerDevice]; dispatch_async(dispatch_get_main_queue(), ^{ [aiReg

我开始使用GCD,我需要知道某个线程何时结束了它的工作

我的代码:

dispatch_queue_t registerDeviceQueue = dispatch_queue_create("RegisterDevice", NULL);
dispatch_async(registerDeviceQueue, ^{
    [self registerDevice];

    dispatch_async(dispatch_get_main_queue(), ^{
        [aiRegisterDevice stopAnimating];
    });
});
dispatch_release(registerDeviceQueue);
我需要知道这一步何时结束,以便UIActivityView可以停止。 现在的情况是,它在线程结束之前停止

谢谢


RL

我会设置一个组,并使用
dispatch\u group\u wait
仅在计算完成后继续。以你为例:

dispatch_queue_t registerDeviceQueue = dispatch_queue_create("RegisterDevice", NULL);
dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group, registerDeviceQueue, ^{
    [self registerDevice];
});

dispatch_group_wait(group, DISPATCH_TIME_FOREVER); // Block until we're ready
// Now we're good to call it:
[aiRegisterDevice stopAnimating];

dispatch_release(registerDeviceQueue);
dispatch_release(group);
或者,如果要防止回调阻塞,请使用
dispatch\u group\u notify

dispatch_queue_t registerDeviceQueue = dispatch_queue_create("RegisterDevice", NULL);
dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group, registerDeviceQueue, ^{
    [self registerDevice];
}); // In this version, the group won't block


// This block gets called asynchronously when the above code is done:
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    [aiRegisterDevice stopAnimating];
});

dispatch_release(registerDeviceQueue);
dispatch_release(group);

我会设置一个组,并使用
dispatch\u group\u wait
仅在计算完成后继续。以你为例:

dispatch_queue_t registerDeviceQueue = dispatch_queue_create("RegisterDevice", NULL);
dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group, registerDeviceQueue, ^{
    [self registerDevice];
});

dispatch_group_wait(group, DISPATCH_TIME_FOREVER); // Block until we're ready
// Now we're good to call it:
[aiRegisterDevice stopAnimating];

dispatch_release(registerDeviceQueue);
dispatch_release(group);
或者,如果要防止回调阻塞,请使用
dispatch\u group\u notify

dispatch_queue_t registerDeviceQueue = dispatch_queue_create("RegisterDevice", NULL);
dispatch_group_t group = dispatch_group_create();

dispatch_group_async(group, registerDeviceQueue, ^{
    [self registerDevice];
}); // In this version, the group won't block


// This block gets called asynchronously when the above code is done:
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    [aiRegisterDevice stopAnimating];
});

dispatch_release(registerDeviceQueue);
dispatch_release(group);

在第一次
调度\u async()
之前,您是否正在调用
[AirRegisterDevice startAnimating]
?发生在哪里?嗨,是的。我正在打电话给VIEWWILLEXECE。是的,我正在打电话给-VIEWWILLEXECE。问题是在[self-registerDevice]中,我调用一个web服务并从中接收数据。我看到状态栏的ActivityIndicator在我的AI停止后工作。。。这意味着我仍然接收数据……这意味着
registerDevice
执行异步请求,因为在方法结束之前不会调用
stopAnimating
。您应该在成功/错误下载回调中调用
stop animating
。很难真正回答您的问题,因为
[self registerDevice]
可以做任何事情……因此,最好调用的类有一个委托方法,将该类设置为委托,然后调用该委托停止动画制作。正确吗?您是否在第一次
调度\u async()
之前调用
[AirRegisterDevice startAnimating]
?发生在哪里?嗨,是的。我正在打电话给VIEWWILLEXECE。是的,我正在打电话给-VIEWWILLEXECE。问题是在[self-registerDevice]中,我调用一个web服务并从中接收数据。我看到状态栏的ActivityIndicator在我的AI停止后工作。。。这意味着我仍然接收数据……这意味着
registerDevice
执行异步请求,因为在方法结束之前不会调用
stopAnimating
。您应该在成功/错误下载回调中调用
stop animating
。很难真正回答您的问题,因为
[self registerDevice]
可以做任何事情……因此,最好调用的类有一个委托方法,将该类设置为委托,然后调用该委托停止动画制作。正确吗?我们应该调用
dispatch\u release
inside
dispatch\u group\u notify
block吗?我们应该调用
dispatch\u release
inside
dispatch\u group\u notify
block吗?