放置代码到空闲线程执行
Posted 子柱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了放置代码到空闲线程执行相关的知识,希望对你有一定的参考价值。
static inline void onIdleThreadAsync(void (^block)()) { if (![NSThread isMainThread]) { block(); return; } static NSOperationQueue *queue; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ queue = [NSOperationQueue new]; }); CFUUIDRef uuidRef = CFUUIDCreate(NULL); CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef); CFRelease(uuidRef); NSString *uuidValue = (__bridge NSString *)uuidStringRef; NSString *name = [NSString stringWithFormat:@"EnqueueIdleThreadNotification_%@", uuidValue]; NSNotification *notification = [NSNotification notificationWithName:name object:nil]; id __block token = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:queue usingBlock:^(NSNotification * _Nonnull note) { block(); [[NSNotificationCenter defaultCenter] removeObserver:token]; }]; [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostWhenIdle]; }
以上是关于放置代码到空闲线程执行的主要内容,如果未能解决你的问题,请参考以下文章