This is not about "unable to enumerate assets on devices", because of the async of enumerating ALAssetsGroup
.
The Async
According to apple's document about enumerateGroupsWithTypes:usingBlock:failureBlock:
of ALAssetsLibrary
, this is an async method, will not get full data stored in array after it run.
This method is asynchronous. When groups are enumerated, the user may be asked to confirm the application's access to the data; the method, though, returns immediately. You should perform whatever work you want with the assets in enumerationBlock.
Completion Notification
I do want to know when it is done. so that I have found an answer for knowing when the progress is done, and execute a block (cf. Create my own completion blocks in iOS). Even though this is not about the notification, but still gave me a hint to work it out.
postNotification:
while it reach the end of the enumeration
[_assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if(group){
// add into the array you are going to use
}
else
{
// the last one will be nil,
// it also means the enumeration is done
// post notification then
[[NSNotificationCenter defaultCenter] postNotificationName:kEnumerateGroupCompleteNotification];
}
}
failureBlock:nil]; // leave nil here for make subject out
addObserver:selector:name:object:
to add observer for reloading data
First, using the empty NSArray
, which is the retained array we are using at previous step, in the ~DataSource
protocol of UICollectionView
or UITableView
.
Second, adding an observer to the UIViewController
, using @selector
pass to this method to trigger the instance's reloadData
to get the complete array just enumerating out. And the data will be shown on the view.
// In a UIViewController's implementation
- (void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadTableView:)
name:kEnumerateGroupCompleteNotification
object:nil];
}
-(void)reloadTableView:(NSNotification*) notification {
// reloadData after receive the notification
[_tableView reloadData];
}
Conclusion
This is the way how I achieve my requirement. I have addressed ALAssetsGroup
only above, but it will be the same in ALAssets
's enumeration.
It is the same way if you want to get ALAssetsLibraryChangedNotification
and reload data.
And a great thank for Ramshad's answer, I have accessing ALAssetsLibrary
as your suggestion in my static library now.