gpt4 book ai didi

ios - 在主线程上发布 NSNotification

转载 作者:IT老高 更新时间:2023-10-28 11:30:13 25 4
gpt4 key购买 nike

我发现下面的代码片段允许 NSNotification 从任何后台线程发布到主线程。我想知道这是否是一种安全且可接受的做法?

dispatch_async(dispatch_get_main_queue(),^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"ImageRetrieved"
object:nil
userInfo:imageDict];
});

最佳答案

是的,你可以

通常,您希望在 main 上发送 NSNotifications,尤其是当它们触发 UI 事件(例如关闭模式登录对话框)时。

Delivering Notifications To Particular Threads

Regular notification centers deliver notifications on the thread in which the notification was posted. Distributed notification centers deliver notifications on the main thread. At times, you may require notifications to be delivered on a particular thread that is determined by you instead of the notification center. For example, if an object running in a background thread is listening for notifications from the user interface, such as a window closing, you would like to receive the notifications in the background thread instead of the main thread. In these cases, you must capture the notifications as they are delivered on the default thread and redirect them to the appropriate thread.

关于ios - 在主线程上发布 NSNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15813764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com