gpt4 book ai didi

macos - 将通知中心与执行选择器一起使用

转载 作者:行者123 更新时间:2023-12-03 17:50:48 25 4
gpt4 key购买 nike

我想从通知中心获取非主线程的通知。在将观察者添加到NotificationCenter时,有什么方法可以使用performselector onThread吗?

最佳答案

您必须使用要处理通知的 dispatch_queue_t 设置 NSOperationQueue。以下是注册当前区域设置更改通知的示例:

- (instancetype)init
{
self = [super init];
if (self)
{
//You need to set this variable to the queue you want the blocks to run on if not on default background queue
dispatch_queue_t queueToPostTo = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

//Properties being used
//@property (nonatomic, strong) NSObject * localeChangeObserver;
//@property (nonatomic, strong) NSOperationQueue * localChangeObserverQueue;

self.localChangeObserverQueue = [[NSOperationQueue alloc] init];
[self.localChangeObserverQueue setUnderlyingQueue:queueToPostTo];

NSNotificationCenter * notificationCenter = [NSNotificationCenter defaultCenter];
self.localeChangeObserver = [notificationCenter addObserverForName:NSCurrentLocaleDidChangeNotification
object:nil
queue:self.localChangeObserverQueue
usingBlock:^(NSNotification *note) {
//Your code here for processing notification.
}];
}
return self;
}
- (void)dealloc
{
NSNotificationCenter * notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self.localeChangeObserver];
}

关于macos - 将通知中心与执行选择器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27624154/

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