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

时间:2012-12-10 16:29:21

标签: core-data concurrency nsmanagedobjectcontext

我在核心数据应用程序中添加了一些名为Transaction的对象。这些交易与帐户相关联。

我想在保存所有交易时更新帐户金额。有时会出现并发异常

  

“集合在被枚举时发生变异”

NSArray *matches = [managedObjectContext executeFetchRequest:request error:&error];方法的accountManagedObjectWithId行。

//TransactionsManager

- (BOOL)addRepeatTransaction:(Transaction *)transaction{

    Account *accountTrx = transaction.account;
    double accountAmount = accountTrx.amount;
    for (int i =0; i<nbRepeat; i++){
         accountAmount +=transactionBiz.amount;
        [[Persister instance] registerTransaction:transaction];
    }
    [[Persister instance]editAmountAccount:transaction.account amount:accountAmount];
    [[Persister instance]saveContext];

    return YES;
}

//Persister

-(id)init {
    if (self = [super init]){
        managedObjectContext = [appDelegate managedObjectContext];
        return self;
    }
    return nil;
}

-(BOOL)registerTransaction:(Transaction *)transaction {
    TransactionManagedObject *transactionsRow = (TransactionsManagedObject *)[NSEntityDescription insertNewObjectForEntityForName:@"Transactions" inManagedObjectContext:managedObjectContext];

    transactionsRow.idTransaction =  [NSNumber numberWithInt:transaction.idTransaction];
    transactionsRow.name = transaction.name;
    transactionsRow.amount = [NSNumber numberWithDouble:transaction.amount];
    [...]
    transactionsRow.account = [[Finder instance] accountManagedObjectWithId:transaction.account.idAccount];

    return YES;
}

-(BOOL)editAmountAccount:(Account *)asset amount:(double)amount {
    AccountManagedObject *accountRow = [[Finder instance] accountManagedObjectWithId:account.idAccount];

    accountRow.amount = [NSNumber numberWithDouble:amount];
    return YES;
}

-(AccountManagedObject *)accountManagedObjectWithId:(NSInteger)idAccount {

    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request setEntity:[NSEntityDescription entityForName:@"Accounts" inManagedObjectContext:managedObjectContext]];

    //Predicate
    NSString *recordedIdAccount = @"idAccount";
    NSNumber *numberIdAccount = [NSNumber numberWithInt:idAccount];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", recordedIdAccount,numberIdAccount];
    [request setPredicate:predicate];

    //Execute
    NSError *error;
    NSArray *matches = [managedObjectContext executeFetchRequest:request error:&error];
    NSInteger nbResult = [matches count];
    if(nbResult==1){
        return [matches objectAtIndex:0];
    }
    if(nbResult==0){
        [...]
        return nil;
    }
    if(nbResult>0){
        [...]
        return nil;
    }
    return nil;
}

//UI call
[...]
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
     //Save
     [[TransactionsManager instance] addRepeatTransaction:transactionToAdd];

     dispatch_async(dispatch_get_main_queue(), ^{

          //Call the delegate
          [self.delegate theSaveButtonOnTheAddTransactionTVCWasTapped:self];
      });
});

@Valentin Radu

我试过了,但问题仍然存在。我必须为每次添加设置managedObjectContext。这是有效的,但它会减慢应用程序。这是正确的方法吗?

-(BOOL)registerTransaction:(Transaction *)transaction{
    NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
   [moc setPersistentStoreCoordinator:[[self managedObjectContext] persistentStoreCoordinator]];


    TransactionsManagedObject *transactionsRow = (TransactionsManagedObject *)[NSEntityDescription insertNewObjectForEntityForName:@"Transactions" inManagedObjectContext:moc];

    transactionsRow.idTransaction =  [NSNumber numberWithInt:transaction.idTransaction];
    transactionsRow.name = transaction.name;
    transactionsRow.amount = [NSNumber numberWithDouble:transaction.amount];
    [...]
    transactionsRow.account = [[Finder instance] accountManagedObjectWithId:transaction.account.idAccount andManagedContext:moc]];

    NSError *error = nil;
    if (![moc save:&error]) {
        [[ErrorManager instance] addError:error];
        return NO;
    }

    return YES;
}

1 个答案:

答案 0 :(得分:5)

您似乎只使用一个上下文,如果您在核心数据对象上执行并行操作,那么这是错误的。每个线程都应该有一个上下文,并在每次保存时合并上下文。 Apple的核心数据指南更详细地讨论了这一点。