在接收 ALAssetsLibraryChangedNotification 时从 ALAssetsGroupSavedPhotos 组中检索 ALAsset 时应用程序崩溃
Posted
技术标签:
【中文标题】在接收 ALAssetsLibraryChangedNotification 时从 ALAssetsGroupSavedPhotos 组中检索 ALAsset 时应用程序崩溃【英文标题】:app crashes when retrieving the ALAssets from ALAssetsGroupSavedPhotos group up on receiving ALAssetsLibraryChangedNotification 【发布时间】:2014-01-08 11:07:01 【问题描述】:我的应用程序的一部分有一个类似于 Apple 的照片应用程序的照片浏览器(类似网格的视图)。为了在原始照片应用程序发生任何变化时刷新我的照片,我注册了 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】:分配 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;
【讨论】:
以上是关于在接收 ALAssetsLibraryChangedNotification 时从 ALAssetsGroupSavedPhotos 组中检索 ALAsset 时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
为啥 java 多播接收器无法在 Windows XP 上接收数据包?
如何在 Android Studio 中使用广播接收器接收短信?