在我最新的 iOS 应用更新中,我发现与数据库相关的崩溃显着增加。我看到几乎每一行在数据库上运行提取的代码都会崩溃。
例外:
NSRangeException
* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
堆栈跟踪:
CoreFoundation 0x32c982a3 __exceptionPreprocess + 163
1
libobjc.A.dylib 0x3a93d97f objc_exception_throw + 31
2
CoreFoundation 0x32be3b75 -[__NSArrayM objectAtIndex:] + 165
3
CoreData 0x32a4227f -[NSSQLCore _newRowsForFetchPlan:selectedBy:withArgument:] + 2007
4
CoreData 0x32a3b089 -[NSSQLCore newRowsForFetchPlan:] + 313
5
CoreData 0x32a3a73f -[NSSQLCore objectsForFetchRequest:inContext:] + 683
6
CoreData 0x32a3a205 -[NSSQLCore executeRequest:withContext:error:] + 469
7
CoreData 0x32a3961d -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 1645
8
CoreData 0x32a37f17 -[NSManagedObjectContext executeFetchRequest:error:] + 647
9
MyApp 0x000516b7 +[DBQuery searchObjectsWithEntityName:::::] + 211
10
MyApp 0x000515a1 +[DBQuery syncData] + 113
11
MyApp 0x0006a7f7 __14+[MyAppSync sync]_block_invoke + 39
12
libdispatch.dylib 0x3ad5511f _dispatch_call_block_and_release + 11
13
libdispatch.dylib 0x3ad63259 _dispatch_root_queue_drain + 261
14
libdispatch.dylib 0x3ad633b9 _dispatch_worker_thread2 + 85
15
libsystem_c.dylib 0x3ad89a11 _pthread_wqthread + 361
16
libsystem_c.dylib 0x3ad898a4 start_wqthread + 8
看起来罪魁祸首的代码行可能与添加我的 NSFetchRequest 的两行修改有关:
+ (NSFetchRequest*) getFetchRequestForEntityName:(NSString*)entityName :(NSPredicate*)predicate :(NSString*)sortKey :(BOOL)sortAscending :(int)limit :(NSManagedObjectContext*)managedObjectContext
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
[request setFetchLimit:limit];
//* NEW IN LATEST RELEASE **//
[request setIncludesPropertyValues:NO];
[request setReturnsObjectsAsFaults:NO];
//**************//
if(predicate != nil)
{
[request setPredicate:predicate];
}
// If a sort key was passed, use it for sorting.
if(sortKey != nil)
{
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:sortAscending];
NSArray *sortDescriptors = @[sortDescriptor];
[request setSortDescriptors:sortDescriptors];
}
return request;
}