Forked from teofiloisrael/[ALAssetsLibrary enumerateGroupsWithTypes:] Asynchronous
Last active
August 29, 2015 14:11
-
-
Save tettoffensive/df6a38b6b37ea40a6809 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Wait for [AssestLibrary enumerateGroupsWithTypes: usingBlock:] to be completed, or how other | |
* people call it, [AssestLibrary enumerateGroupsWithTypes: usingBlock:] Asynchronous | |
* The following method should not be called on main thread, use | |
* [self performSelectorInBackground:@selector(getAllAssets) withObject:nil]; | |
* to excecute it | |
*/ | |
- (void)getAllAssets | |
{ | |
data_ = [[NSMutableArray alloc] init]; | |
enum { | |
WDASSETURL_PENDINGREADS = 1, | |
WDASSETURL_ALLFINISHED = 0 | |
}; | |
NSAssert(![NSThread isMainThread], @"can't be called on the main thread due to ALAssetLibrary limitations"); | |
NSConditionLock* photosUrlReadLock = [[NSConditionLock alloc] initWithCondition:WDASSETURL_PENDINGREADS]; | |
ALAssetsLibrary *assestLibrary = [AssetsLibraryManager defaultAssetsLibrary]; | |
[assestLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop){ | |
if (group == nil) { | |
[photosUrlReadLock lock]; | |
[photosUrlReadLock unlockWithCondition:WDASSETURL_ALLFINISHED]; | |
} | |
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { | |
ALAssetRepresentation *representation = [result defaultRepresentation]; | |
if ([representation url]) { | |
[data_ addObject:[representation url]]; | |
} | |
}]; | |
} failureBlock:^(NSError *error) { | |
NSLog(@"error: %@", error); | |
[photosUrlReadLock lock]; | |
[photosUrlReadLock unlockWithCondition:WDASSETURL_ALLFINISHED]; | |
}]; | |
[photosUrlReadLock lockWhenCondition:WDASSETURL_ALLFINISHED]; | |
[photosUrlReadLock unlock]; | |
//Do other things here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment