When an ALAssetsGroup is large, there is a delay of several seconds between the time enumerateAssetsUsingBlock: or enumerateAssetsAtIndexes: is called and the time the first enumeration block is called.
On an iPad 2 with 10000 assets in the library, this delay is approximately 4.2 seconds before the first asset block is called.
Photos.app on the same device with the same library starts in less than a second showing all photos in the library. I have no idea how it does this. That's the kind of behaviour we would like to see.
This is a minimal test case that shows the behaviour:
ALAssetsLibrary* assetLibrary = [ALAssetsLibrary new];
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupLibrary
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group != nil) {
NSLog(@"*** Got library group, start timer");
NSDate* tic = [NSDate date];
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:0] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if (result==nil) {
NSLog(@"*** Elapsed time to final enumeration: %.2fs", -[tic timeIntervalSinceNow]);
}
else if (index==0) {
NSLog(@"*** Elapsed time to image 0: %.2fs", -[tic timeIntervalSinceNow]);
}
}];
*stop = YES;
}
}
failureBlock:^(NSError *error)
{
NSLog(@"ERROR: %@", [error localizedDescription]);
}];
Output:
*** Got library group, start timer
*** Elapsed time to image 0: 4.20s
*** Elapsed time to final enumeration: 4.20s
(Note that the solution accepted in this (slightly different) question does not work. The delay happens before the first asset is even reached: Possible ways to speed up reading from ALAssetsLibrary and populating a UITableView )