核心数据 - 集合__NSCFSet在枚举时发生了变异

时间:2015-03-20 12:11:12

标签: ios objective-c multithreading core-data

将数据插入核心数据时应用程序崩溃。 有超过500条记录要插入数据库。如果messageId没有在数据库中退出,则首先检查条件,然后在数据库中插入记录。 但是应用程序崩溃

BOOL isExist = [context countForFetchRequest:request error:& error];

错误集< __ NSCFSet:0x16f601c0>在被列举时发生变异。'

 appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    NSManagedObjectContext *context = appDelegate.managedObjectContext;
    __block NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
    __block NSManagedObjectContext *writerObjectContext = appDelegate.writerManagedObjectContext;
    __block NSManagedObjectContext *temporaryContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    temporaryContext.parentContext = managedObjectContext;
    [temporaryContext performBlockAndWait:^{
        NSError *error;
        for (StreamData *stream in streamData) {
            NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:DB_TABLE_STREAM];
            [request setPredicate:[NSPredicate predicateWithFormat:@"stream_id = %@", stream.messageId]];
            [request setFetchLimit:1];
            BOOL isExist = [context countForFetchRequest:request error:&error];

            if (!isExist) {
                CF_Stream *object = [NSEntityDescription insertNewObjectForEntityForName:DB_TABLE_STREAM inManagedObjectContext:context];
                [object setStream_id:[NSNumber numberWithLongLong:[stream.messageId longLongValue]]];
                [object setType:stream.streamType];
                [object setTime_stamp:[[DateTimeManager sharedInstances] getStreamDate:stream.streamTime]];
            }
            if (![temporaryContext save:&error]) {
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            }
        }
        if (![temporaryContext save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
        [managedObjectContext performBlock:^{
            NSError *error = nil;
            if (![managedObjectContext save:&error]) {

                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            }
            [writerObjectContext performBlock:^{
                NSError *error = nil;
                if (![writerObjectContext save:&error])  {
                    completionBlock(NO);
                    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                    abort();
                } else {
                    completionBlock(YES);
                }
            }];
        }];
    }];

0 个答案:

没有答案