Listing ALAssets Groups: How to Verify Completion?

ALAssets use a dedicated thread to control the enumeration, I need to know when the enumeration ends.

Block prototype for listing groups:

typedef void (^ALAssetsLibraryGroupsEnumerationResultsBlock)(ALAssetsGroup *group, BOOL *stop) ;

How to add a completion block?

+6
source share
2 answers

I found a solution that is only partially described.

When the group enumeration completes, ALAssetsLibraryGroupsEnumerationResultsBlock is called with group = nil. So you can write something like:

 void (^groupsEnumerator)(ALAssetsGroup *,BOOL *) = ^(ALAssetsGroup *group, BOOL *stop){ if (group != nil) { [group enumerateAssetsUsingBlock:assetsEnumerator]; }else { NSLog(@"group enumeration terminated"); } }; 

The same decision applies to asset transfers (this is not documented -.-)

  void (^assetsEnumerator)(ALAsset *,NSUInteger,BOOL*) = ^(ALAsset *result, NSUInteger index, BOOL *stop){ if (result !=nil) { //do something with result asset }else { NSLog(@"Assets enumeration terminated"); } }; 
+9
source

I use this:

  [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { if (result == nil) { return; } if (index + 1 == group.numberOfAssets) { //Do what you want. Im using delegate to notify my parent class about finish. [delegate didGroupEnumerated:group]; } }]; 
+1
source

Source: https://habr.com/ru/post/886872/


All Articles