码迷,mamicode.com
首页 > 编程语言 > 详细

放置代码到空闲线程执行

时间:2018-05-29 11:57:04      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:queue   mat   default   UI   idle   when   oid   ada   null   

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];
}

  

放置代码到空闲线程执行

标签:queue   mat   default   UI   idle   when   oid   ada   null   

原文地址:https://www.cnblogs.com/huangzizhu/p/9104161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!