0
[aLib  enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:assetsGroupEnumerationBlock failureBlock:failureBLock];

This method enumerate every group, I want to enumerate for first group only then I want to break it. My purpose is to ask for permission that iOS pop for first time. I am doing no additional work, I have notifications in blocks that notify and trigger other required functionality. But multiple group enumeration trigger notification multiple times and that I want to stop.

Here is my enumeration block with stop parameter

void(^assetsGroupEnumerationBlock)(ALAssetsGroup*, BOOL*) = ^(ALAssetsGroup *groups, BOOL *stop) {
    *stop = YES;
    NSDictionary *alAuthDict = @{@"alAssetsAuthStatusDictKey" : [NSString stringWithFormat:@"%ld",[self getALAssetAuthorizationStatus]]};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"alAssetsStatusNotificationName" object:nil userInfo:alAuthDict];
};

But notification is getting called two times I see nslog twice in console.

S.J
  • 2,979
  • 3
  • 30
  • 63

1 Answers1

2

Use the stop parameter:

[lib enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    *stop = YES;
    if (group) {
        NSDictionary *alAuthDict = @{@"alAssetsAuthStatusDictKey" : [NSString stringWithFormat:@"%ld",[self getALAssetAuthorizationStatus]]};
        [[NSNotificationCenter defaultCenter]postNotificationName:@"alAssetsStatusNotificationName" object:nil userInfo:alAuthDict];
    }
} failureBlock:^(NSError *error) {
    // denied
}];
rmaddy
  • 298,130
  • 40
  • 468
  • 517
  • I am using stop parameter please view the edits in my question. – S.J Apr 25 '14 at 05:05
  • That would have been helpful to include from the beginning. – rmaddy Apr 25 '14 at 05:08
  • why if(group){...} when *stop is set to YES, which indicate to stop, means it should not enumerate any more. – S.J Apr 25 '14 at 05:13
  • It's asynchronous so perhaps it is processing multiple groups at once. Even though you tell it to stop, not all threads are immediately stopped so you get the extra call. Another option would be to add a counter and only post the notification the first time. Or try changing `ALAssetsGroupAll` to a single, specific group like `ALAssetsGroupLibrary`. – rmaddy Apr 25 '14 at 05:16
  • Thanks a lot for the awesome replies :). – S.J Apr 25 '14 at 05:20
  • if(group){...} worked, I was thinking about the counter but not sure that its a good approach, but you point out that its asynchronous and processing multiple groups at once made all things clear. – S.J Apr 25 '14 at 05:26