ALAssetsLibrary错误 - “太多上下文。在contextList中没有空格。”

时间:2013-09-19 18:08:35

标签: ios objective-c sigabrt alassetslibrary

我收到了一些用户的崩溃报告。我甚至不知道从哪里开始。我在一个表视图中显示资产库中的图像,从一个组列表开始。我使用以下代码加载组(在主线程上):

- (ALAssetsLibrary *)library {
    if (!_library) {
        _library = [[ALAssetsLibrary alloc] init];
    }

    return _library;
}

- (void)determineGroups {
    ALAssetsLibrary *lib = [self library];
    [lib enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos | ALAssetsGroupPhotoStream | ALAssetsGroupFaces | ALAssetsGroupLibrary | ALAssetsGroupAlbum | ALAssetsGroupEvent usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if (group) {
            if (!_groups) {
                _groups = [[NSMutableArray alloc] init];
            }

            if ([group numberOfAssets]) {
                [group setAssetsFilter:[ALAssetsFilter allPhotos]];
                if ([[group valueForProperty:ALAssetsGroupPropertyType] isEqual:@(ALAssetsGroupSavedPhotos)]) {
                    [_groups insertObject:group atIndex:0];
                } else {
                    [_groups addObject:group];
                }
            }
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
    } failureBlock:^(NSError *error) {
        // denied
        dispatch_async(dispatch_get_main_queue(), ^{
            self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            [self.tableView reloadData];
        });
    }];
}

填充表格视图时,我想显示每组中的照片数量。我通过在组上调用“numberOfAssets”方法来完成此操作。

这一切在大部分时间都很好,但在极少数情况下崩溃。这是一个带有相关信息的堆栈跟踪(线程5在主线程忙于获取组中的资产数量时崩溃):

Crashed Thread:  5

Application Specific Information:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Too many contexts. No space in contextList.'

Thread 0:
0   CoreFoundation                      0x313a1c42 _CFStringGetLength2 + 26
1   CoreFoundation                      0x313aba41 -[__NSDictionaryM objectForKey:] + 133
2   CoreData                            0x312b5d2f -[NSSQLOrderIntermediate generateSQLStringInContext:] + 1283
3   CoreData                            0x312018f7 -[NSSQLFetchIntermediate generateSQLStringInContext:] + 515
4   CoreData                            0x311fe625 -[NSSQLGenerator newSQLStatementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:] + 425
5   CoreData                            0x311fe3ad -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:] + 509
6   CoreData                            0x311fdfc5 -[NSSQLCore newRowsForFetchPlan:] + 117
7   CoreData                            0x311fd73f -[NSSQLCore objectsForFetchRequest:inContext:] + 683
8   CoreData                            0x311fd205 -[NSSQLCore executeRequest:withContext:error:] + 469
9   CoreData                            0x311fc61d -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 1645
10  CoreData                            0x311faf17 -[NSManagedObjectContext executeFetchRequest:error:] + 647
11  PhotoLibraryServices                0x366cd4e5 -[PLManagedObjectContext executeFetchRequest:error:] + 49
12  CoreData                            0x3121ad07 _faultBatchAtIndex + 623
13  CoreData                            0x3121aa53 -[_PFBatchFaultingArray getObjects:range:] + 143
14  CoreData                            0x3121a9c1 -[_PFBatchFaultingArray getObjects:] + 41
15  Foundation                          0x31cfbbe1 _filterObjectsUsingPredicate + 325
16  Foundation                          0x31d01b8d -[NSArray(NSPredicateSupport) filteredArrayUsingPredicate:] + 273
17  PhotoLibraryServices                0x366d86ad +[PLFilteredAlbum filteredIndexesInAlbum:predicate:] + 265
18  PhotoLibraryServices                0x366d8bd1 -[PLFilteredAlbum filteredIndexes] + 117
19  PhotoLibraryServices                0x366d8d75 -[PLFilteredAlbum countOfFilteredAssets] + 21
20  AssetsLibrary                       0x30ce25d5 __31-[ALAssetsGroup numberOfAssets]_block_invoke_0 + 41
21  CoreData                            0x31257fd1 developerSubmittedBlockToNSManagedObjectContextPerform + 89
22  libdispatch.dylib                   0x3971d4b7 _dispatch_client_callout + 23
23  libdispatch.dylib                   0x397219f7 _dispatch_barrier_sync_f_invoke + 31
24  CoreData                            0x31258153 -[NSManagedObjectContext performBlockAndWait:] + 175
25  AssetsLibrary                       0x30ce414f -[ALAssetsLibrary _performBlockAndWait:] + 135
26  AssetsLibrary                       0x30ce8f59 -[ALAssetsGroupPrivate _performBlockAndWait:] + 177
27  AssetsLibrary                       0x30ce254b -[ALAssetsGroup numberOfAssets] + 259

Thread 5 Crashed:
0   libsystem_kernel.dylib              0x39804350 __pthread_kill + 8
1   libsystem_c.dylib                   0x397b7973 abort + 95
2   MyApp                               0x0074685f ___lldb_unnamed_function30167$$MyApp + 27
3   CoreFoundation                      0x3145b57f __handleUncaughtException + 615
4   libobjc.A.dylib                     0x39306a65 _objc_terminate() + 129
5   libc++abi.dylib                     0x38d5307b safe_handler_caller(void (*)()) + 79
6   libc++abi.dylib                     0x38d53114 std::terminate() + 20
7   libc++abi.dylib                     0x38d54513 __cxa_throw + 123
8   libobjc.A.dylib                     0x393069bf objc_exception_throw + 95
9   CoreFoundation                      0x3145b15d +[NSException raise:format:] + 1
10  Foundation                          0x31d30b13 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 87
11  PhotoLibraryServices                0x366d0afb __addContextToList_block_invoke_0 + 179
12  libdispatch.dylib                   0x3971e11f _dispatch_call_block_and_release + 11
13  libdispatch.dylib                   0x39721ecf _dispatch_queue_drain$VARIANT$mp + 143
14  libdispatch.dylib                   0x39721dc1 _dispatch_queue_invoke$VARIANT$mp + 41
15  libdispatch.dylib                   0x3972291d _dispatch_root_queue_drain + 185
16  libdispatch.dylib                   0x39722ac1 _dispatch_worker_thread2 + 85
17  libsystem_c.dylib                   0x39752a11 _pthread_wqthread + 361

崩溃来自运行iOS 6.1.3的iPad。该应用程序是使用iOS 7 GM的Base SDK构建的。该应用程序使用ARC。

我的代码中是否应该做些不同的事情来获取群组?

2 个答案:

答案 0 :(得分:3)

  • 绝对是iOS问题,并在enumerateGroupsWithTypes:...方法包含ALAssetsGroupLibrary内部类型枚举时重现。
  • 您必须使用ALAssetsGroupALL(不包括ALAssetsGroupLibrary
  • 或者您可以使用OR枚举类型,但不要将ALAssetsGroupLibrary包括在枚举中。
  • 如果您在类型枚举中确实需要ALAssetGroupLibrary,我不知道该怎么做。

  • 我的代码与你的代码非常相似;如果你想重现这个问题,试着多次调用determineGroups方法(当我这样做11次时,我的问题会重现)但是每次调用这个方法时都必须重新创建库对象(fe你可以在{{{{ 1}}方法。

  • 或者您可以每次为新接收者调用 determineGroups 方法。)。另外,要重现此问题,请不要在单个循环循环中调用determineGroups方法
  • 如果您创建按钮并点按此按钮会创建新的接收器并为其调用determineGroups会更好。现在尝试多次点击此按钮。

希望我的回答能帮助你。

答案 1 :(得分:0)

我通过在使用ALAssetsLibrary时创建一个sigleton对象来解决这个问题。

+ (instancetype)defaultAssetsLibrary
{
    @synchronized(self) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            defaultAssetsLibrary = [[self alloc] init];
        });
    }
    return defaultAssetsLibrary;
}