Reputation: 1497
I want to get notifications on non-main thread from notificationcenter. is there any way I can use performselector onThread when adding observer to NotificationCenter?
Upvotes: 0
Views: 175
Reputation: 7381
You have to set up a NSOperationQueue
using the dispatch_queue_t
you want to process notifications on. Here's an example of registering for current locale changed notification:
- (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];
}
Upvotes: 1