2

我在后台线程中使用核心数据,当使用 NSFetchRequest 在迭代中查询数据时,执行 [context executeFetchRequest: error:] 时总是出现此错误:

2013-10-13 10:23:37.052 EExFab[157:217] CoreData: error: exception during newFetchedPKsForSourceID: statement is still active with userInfo of (null)
2013-10-13 10:23:37.056 EExFab[157:217] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'statement is still active'
*** First throw call stack:
(0x2fe76f53 0x3a24f6af 0x2fbc7999 0x2fbc71e5 0x2fbe0ef7 0x2fbd67ed 0x2fbcfa6f 0x2fbf9fcf    0x2fbf91f1 0x2fbf8e21 0x2fbf8a5b 0x2fbfb715 0x2fbf7d2b 0x2fbf7b73 0x307a59a1 0x307a5645 0x30791c5d 0x2fbefa03 0x2fbee3b3 0x9b9a1 0x2fbdc5a9 0x2fbdb4cf 0x2fca4ab3 0x2fca119b 0x2fbcf085 0x2fbceb33 0x2fbce3e5 0x2fbcc7e7 0xa9d69 0xa95ad 0xa4f0b 0xa9511 0x30859e27 0x3a876c1d 0x3a876b8f 0x3a874c90)
libc++abi.dylib: terminating with uncaught exception of type NSException

代码是这样的:

@implementation EExFab{
    NSString *filePath;
    NSManagedObjectModel *model;
    NSPersistentStoreCoordinator *coordinator;
    NSManagedObjectContext *context;
    NSFetchRequest *fetchRequest;
    NSArray *fetchResult;
}

-(void)prepareCoreDataVariables{
    NSError *error;
    NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES};
    if(!filePath)filePath=[[NSHomeDirectory()    stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"CDStoreOfEExFab.CDABStore"];
    if(!model)model=[[NSManagedObjectModel alloc]initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"EExFabModel" withExtension:@"momd"]];
    if(!coordinator){
        coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: model];
        if(![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:filePath isDirectory:NO] options:options error:&error]){
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        }
    }
    if(!context){
        context=[[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
        [context setPersistentStoreCoordinator: coordinator];
        [context setUndoManager:nil];
    }
    if(!fetRequest)fetchRequest = [[NSFetchRequest alloc] init];
}

-(void)performBackgroundTask{ // invoker
    [NSThread detachNewThreadSelector:@selector(onPerformBackgroundTask) toTarget:self withObject:nil];
}

-(void)onPerformBackgroundTask{
    [self prepareCoreDataVariables];
    // .... perform creation of Parsing some data into Core Data Storage using the variables
    // .... NO error occurs during this period. ....
    [self prepareCoreDataVariables];
    for (id obj in someFetchedArray){
        fetchRequest=[[NSFetchRequest alloc]init];
        [fetchRequest setEntity:[NSEntityDescription entityForName:@"EExFabOBJ" inManagedObjectContext:context]];
        [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(timeOfCreation == %@)",obj.timeOfCreation]];
        NSError *err=nil;
        // ***************************CRASH*********************************************
        fetchResult=[context executeFetchRequest:fetchRequest error:&err]; // <<<<<CRASH
        // ***************************CRASH*********************************************
        // .... other things ...
    }
}

应用程序在第一次执行上述代码时崩溃,没有其他线程或块使用或使用本地 Core Data 变量。实际上,这些变量是由后台线程本身初始化的。

请注意崩溃迭代正上方的块,核心数据存储被初始化并解析为在该块中创建,它们在同一个线程中。

请帮忙,谢谢。

4

0 回答 0