iOS根据调用队列在特定队列上调度

时间:2015-04-27 21:08:57

标签: ios objective-c

某些Apple API函数如何根据调用队列在特定队列上实现调用回调块?

例如,当从主队列调用[ALAssetsLibrary enumerateGroupsWithTypes:usingBlock:failureBlock:]函数时,也会在主队列上调用结果块,而在从全局/后台队列或自定义队列调用此函数时,结果块在默认优先级全局队列(com.apple.root.default-qos)上调用。

显然,dispatch_get_current_queue()函数可以完成这项工作,但是自iOS6以来该函数已被弃用。

以下示例代码:

dispatch_async(dispatch_get_main_queue(), ^{
    ALAssetsLibrary *library = [ALAssetsLibrary new];
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        NSLog(@"%@", group); //gets called on main queue
    } failureBlock: nil];
});

dispatch_async(dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL), ^{
    ALAssetsLibrary *library = [ALAssetsLibrary new];
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        NSLog(@"%@", group); //gets called on com.apple.root.default-qos queue
    } failureBlock: nil];
});

0 个答案:

没有答案