【问题标题】:Need assistance regarding ALAssetsLibrary enumeration需要有关 ALAssetsLibrary 枚举的帮助
【发布时间】:2014-04-25 04:59:14
【问题描述】:
[aLib  enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:assetsGroupEnumerationBlock failureBlock:failureBLock];

这个方法枚举每个组,我想枚举第一组,然后我想打破它。我的目的是请求允许 iOS 首次弹出。我没有做任何额外的工作,我在块中有通知,通知和触发其他必需的功能。但是多组枚举多次触发通知,我想停止。

这是我的带有停止参数的枚举块

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];
};

但是通知被调用了两次,我在控制台中看到了两次nslog

【问题讨论】:

    标签: ios objective-c alassetslibrary alassetsgroup


    【解决方案1】:

    使用stop参数:

    [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
    }];
    

    【讨论】:

    • 我正在使用停止参数,请查看我的问题中的编辑。
    • 从一开始就包括在内会很有帮助。
    • why if(group){...} when *stop 设置为 YES,表示停止,表示不应该再枚举。
    • 它是异步的,所以它可能同时处理多个组。即使您告诉它停止,并非所有线程都会立即停止,因此您会收到额外的调用。另一种选择是添加一个计数器并仅在第一次发布通知。或者尝试将ALAssetsGroupAll 更改为单个特定组,例如ALAssetsGroupLibrary
    • 非常感谢您的精彩回复 :)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2019-03-26
    • 2015-06-23
    • 2014-12-26
    • 2013-08-12
    • 2014-06-11
    相关资源
    最近更新 更多