I am querying the asset library with the following code
ALAssetsLibrary *assetsLibrary = [self defaultAssetsLibrary];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSMutableArray *tmpAssets = [@[] mutableCopy];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result)
{
[tmpAssets addObject:result];
}
}];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO];
finished([[tmpAssets sortedArrayUsingDescriptors:@[sort]] mutableCopy]);
} failureBlock:^(NSError *error) {
finished(@[]);
NSLog(@"Error loading images %@", error);
}];
However it seems that I am not getting all of the assets, if I add images to the iPhone from an external source these are not showing in the list of assets received from the above code... I am getting what seems like everything else though. I suspected it could be a limitations of the framework but when I use instagram I noticed that the photos which were missing in my app were visible... How do i get every type of asset "ALAssetsGroupAll" not enough?
The problem is that you're not understanding how
enumerateAssetsUsingBlock:
works. It is asynchronous.To see what I mean by that, let me divide up your code using some numbers:
The problem is that 3 executes before 2. Thus there is nothing in
tmpAssets
yet.So what are you supposed to do about this? Well, this method (and other ALAssets methods that work similarly) operate in a very strange and special way: they call their block one extra time, with a nil argument. (You seem to have realized this, since you are doing a nil test; but you have not understood what the implications are.) Your job is to test for a nil parameter. If the parameter is nil, this is the final extra call, and now you can do any cleanup. So, like this: