4

我正在创建一个这样的串行后台队列:

@property (nonatomic, strong) dispatch_queue_t assetCreationQueue;

// in init...
_assetCreationQueue = dispatch_queue_create("com.mycompany.assetCreationQueue", DISPATCH_QUEUE_SERIAL);

然后我在后台枚举 ALAsset 对象,如下所示:

[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
    if (asset){
        dispatch_async(weakSelf.assetCreationQueue, ^{
            ALAssetRepresentation *assetRepresentation = [asset defaultRepresentation];
            NSURL *url = [assetRepresentation url];
            if (url) {
                AVURLAsset *avAsset = [[AVURLAsset alloc] initWithURL:url options:nil];

                // This NSLog fires! avAsset exists.
                NSLog(@"AVURLAsset %@", avAsset);

                dispatch_async(dispatch_get_main_queue(), ^{
                    // This NSLog NEVER fires.
                    // Also tried dispatch_sync.
                    NSLog(@"add to assets array on main queue");
                    [weakSelf.assets insertObject:avAsset atIndex:0];
                });
            }
        });
    }
}];

assets 数组属性定义为:

@property (nonatomic, strong) NSMutableArray *assets;

当我尝试时,dispatch_sync(dispatch_get_main_queue(), ^{我在控制台中只得到一个NSLog(@"AVURLAsset %@", avAsset);,它表明这dispatch_sync导致了死锁。

但是我怎样才能找出原因呢?我看不出在哪里。assetCreationQueue 是一个后台队列,我只能在主队列上对数组进行操作。

编辑: 这是一个更加简化的测试,它也失败了:

[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
    if (asset){
        dispatch_async(weakSelf.assetCreationQueue, ^{
            if ([NSThread isMainThread]) {
                NSLog(@"already main thread"); // gets called often!!
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSLog(@"dispatch async on main queue"); // never gets called!!
                });
            }
        });
     }
}];

所以我不明白的是:为什么我已经在主线程上,即使我调用dispatch_async(weakSelf.assetCreationQueue. 它只能导致邪恶的结论:我创建的队列不是后台队列:

_assetCreationQueue = dispatch_queue_create("com.mycompany.assetCreationQueue", DISPATCH_QUEUE_SERIAL);

为什么?

4

1 回答 1

4

(不是真正的答案,但我想提供足够的信息来继续前进。)

我尝试了一个非常简单的程序,但根本无法重现。我收到很多关于“在主队列上调度异步”的电话,而没有对“已经是主线程”的电话(与您的期望相反)。我在 iPhone 5 上运行它。完整代码:

#import "AppDelegate.h"
@import AssetsLibrary;

@interface AppDelegate ()
@property (nonatomic, strong) dispatch_queue_t assetCreationQueue;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  self.assetCreationQueue = dispatch_queue_create("com.mycompany.assetCreationQueue", DISPATCH_QUEUE_SERIAL);

  ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
  AppDelegate __weak *weakSelf = self;
  [library enumerateGroupsWithTypes:ALAssetsGroupAll
                         usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                           [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
                             if (asset){
                               dispatch_async(weakSelf.assetCreationQueue, ^{
                                 if ([NSThread isMainThread]) {
                                   NSLog(@"already main thread"); // gets called often!!
                                 } else {
                                   dispatch_async(dispatch_get_main_queue(), ^{
                                     NSLog(@"dispatch async on main queue"); // never gets called!!
                                   });
                                 }
                               });
                             }
                           }];
                         }
                       failureBlock:nil];
  return YES;
}
@end

请注意,weakSelf此处的使用是不必要且危险的(但如果这是问题所在,您将崩溃)。仅weakSelf当块将存储在self保留对象的属性中时才需要。像这样的短命块没有必要甚至不需要。你使用它的方式,如果拥有对象在枚举中间被释放(它可以),weakSelf将变为 nil,weakSelf.assetCreationQueue将是nil,并且dispatch_async()会崩溃。(但同样,这不太可能是您的问题的原因,因为它应该崩溃。)

于 2013-09-30T15:23:42.177 回答