在收到ALAssetsLibraryChangedNotification时从ALAssetsGroupSavedPhotos组中检索ALAssets时app崩溃

时间:2014-01-08 11:07:01

标签: alassetslibrary photolibrary alassetsgroup

我的应用程序的一部分有一个类似于Apple的照片应用程序的照片浏览器(Grid like view)。要在原始照片应用程序发生任何更改时刷新我的照片,我注册了ALAssetsLibraryChangedNotification

self.assetsLibrary = [[ALAssetsLibrary alloc] init];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveLibraryChangedNotification:) name:ALAssetsLibraryChangedNotification object:self.assetsLibrary];

在“receiveLibraryChangedNotification”方法中 - 我检查userInfo是否具有ALAssetLibraryUpdatedAssetsKey&然后调用刷新照片方法。

 - (void) receiveLibraryChangedNotification:(NSNotification *) notif{
        NSDictionary *userInfo = [notif userInfo];
        if(userInfo){
            id updatedAssets = [userInfo objectForKey:ALAssetLibraryUpdatedAssetsKey];

            if(updatedAssets){

                   [self refreshPhotos];
                }
    }




- (void) refreshPhotos {

    self.assetArray = nil;

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(void){
                [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                    if(group){
                        [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [group numberOfAssets])] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *shouldStop) {
                            if(result){

                                if([[result valueForProperty:@"ALAssetPropertyType"] isEqualToString:@"ALAssetTypePhoto"]){
                                    [self.assetArray addObject:result];
                                }
                            }
                        }];
                    }
                } failureBlock:^(NSError *error) {

                    DebugLog(@"error >>> %@",[error description]);

                }];
            });
}

我面临的问题是,有时候通知被多次触发&该应用程序崩溃

[self.assetArray addObject:result];

有错误 -

malloc: *** error for object 0x4aa9000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

malloc: *** error for object 0x16fd3e74: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

有时候我没有收到通知的userInfo中的ALAssetLibraryUpdatedAssetsKey,因此照片永远不会刷新。

可以指导我一个正确的方向。

提前致谢。

1 个答案:

答案 0 :(得分:0)

self.assetArray转让后

分配 nil或写下[self.assetArray removeAllObjects],而不是将其分配给nil

- (void) refreshPhotos {

    self.assetArray = nil;
    self.assetArray = [[NSMutableArray alloc] init];

    //or remove the top 2 lines and try `[self.assetArray removeAllObjects]`

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(void){
            [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                if(group){
                    [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [group numberOfAssets])] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *shouldStop) {
                        if(result){

                            if([[result valueForProperty:@"ALAssetPropertyType"] isEqualToString:@"ALAssetTypePhoto"]){
                                [self.assetArray addObject:result];
                            }
                        }
                    }];
                }
            } failureBlock:^(NSError *error) {

                DebugLog(@"error >>> %@",[error description]);

            }];
        });
}
  

有时候我没有收到ALAssetLibraryUpdatedAssetsKey   在通知的userInfo中,

有四个键用于从ALAssetsLibraryChangedNotification通知的用户信息字典中获取值。

NSString * const ALAssetLibraryUpdatedAssetsKey;
NSString * const ALAssetLibraryInsertedAssetGroupsKey;
NSString * const ALAssetLibraryUpdatedAssetGroupsKey;
NSString * const ALAssetLibraryDeletedAssetGroupsKey;