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.
Allocate
self.assetArray
after thenil
assignment or write[self.assetArray removeAllObjects]
instead of assigning it tonil
.there are four keys used to get values from the user information dictionary of the
ALAssetsLibraryChangedNotification
notification.