Is ALAssetsLibrary thread safe (Deadlock occurs when using multiple threads)

680 Views Asked by At

I'm currenly working on a tiny project which amis to loading ALL gallery photos into my app to show some fancy effect. Unfortunately, these default thumbnail provided by system cannot meet my requirement. So I try to create my own thumbnails using "fullScreenImage". To speed up the process, I load fullScreenImage using background operations. The main methods are:

- (void)getFullScreenImage:(NSURL *)url success:(void(^)(UIImage *))callback
{
   NSLog(@"Requesting %@", url);
    [assetsLibraryInstance assetForURL:url resultBlock:^(ALAsset *asset) {
        callback(asset.defaultRepresentation.fullScreenImage);
    }
    failureBlock:nil];
}

- (void)processURLs:(NSArray *)urls
{
    for (NSURL *url in urls) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) ^{
             [self getFullScreenImage:url success:^(UIImage *img) {
                  NSLog(@"Got image %@", img);
             }] ;
        });
    }
}

Only the "Requesting..." log is printed in console, the "getFullScreenImage" method is locked, no any output.

I tried below methods to work around this issue:

  1. Not sharing assetsLibraryInstance (Didn't work)
  2. Don't dispatch_async when enumeate urls in "processURLs". (Did work, but I don't want to use a signle thread to process all URLs)
  3. Not using global queue, using main queue (Did work, but all these "fullScreenImage" works were doing on UI thread, making UI non-responsive)
  4. Using a private queue created with "dispatch_queue_create". (Didn't work)

So, is ALAssetsLibrary thread safe? I guess it's not... Or, is there any better way I can use to:

  1. Load fullScreenImage in background
  2. Multiple threading

Thanks!

0

There are 0 best solutions below