app crashes when retrieving the ALAssets from ALAssetsGroupSavedPhotos group up on receiving ALAssetsLibraryChangedNotification

StackOverflow https://stackoverflow.com/questions/20993826

Question

Part of my app has a photo browser similar to Apple's Photos app (Grid like view). To refresh my photos whenever there is any change in original photo app, i registered for ALAssetsLibraryChangedNotification

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

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

In "receiveLibraryChangedNotification" method - i check if the userInfo has ALAssetLibraryUpdatedAssetsKey & then call the refresh photos method.

 - (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]);

                }];
            });
}

Problem i am facing is that some times the notification is being triggered multiple times & the app crashes at

[self.assetArray addObject:result];

with error -

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

or

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

also some times i am not receiving the ALAssetLibraryUpdatedAssetsKey in the userInfo of notification, so the photos are never refreshed.

can some one guide me in correct direction.

Thanks in advance.

Was it helpful?

Solution

Allocate self.assetArray after the nil assignment or write [self.assetArray removeAllObjects] instead of assigning it to 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]);

            }];
        });
}

also some times i am not receiving the ALAssetLibraryUpdatedAssetsKey in the userInfo of notification,

there are four keys used to get values from the user information dictionary of the ALAssetsLibraryChangedNotification notification.

NSString * const ALAssetLibraryUpdatedAssetsKey;
NSString * const ALAssetLibraryInsertedAssetGroupsKey;
NSString * const ALAssetLibraryUpdatedAssetGroupsKey;
NSString * const ALAssetLibraryDeletedAssetGroupsKey;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top