修改NSFetchRequest,生成NSRangeException索引0超出空数组的边界

时间:2013-06-01 00:51:57

标签: ios core-data nsfetchrequest nsrangeexception

在我最新的iOS App Update中,我发现数据库相关崩溃的数量显着增加。我看到几乎每一行都在DB上运行fetch的代码崩溃。

例外: 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;

[request setFetchLimit:limit];

1 个答案:

答案 0 :(得分:2)

删除两个添加的行可解决问题。

[request setIncludesPropertyValues:NO];
[request setReturnsObjectsAsFaults:NO];