Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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
You can’t perform that action at this time.