核心数据:删除/添加对象时出错 [英] Core Data: Error when deleting/adding objects

查看:259
本文介绍了核心数据:删除/添加对象时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,当我想通过下载和解析json文件更新我的Core Data文件时,我的应用程序崩溃。我收到以下错误:

Sometimes my app crashes when I want to update my Core Data file by downloading and parsing a json file. I get the following error:


CoreData:错误:严重的应用程序错误。在核心数据更改处理期间捕获到
异常。这通常是NSManagedObjectContextObjectsDidChangeNotification的
观察器中的一个错误。
- [__ NSCFSet addObject:]:尝试使用userInfo(null)插入nil

CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[__NSCFSet addObject:]: attempt to insert nil with userInfo (null)

如果我在迭代期间更改属性,那么我在迭代中保存NSManagedObjectContext?

这里是我的代码:

- (void) updateData
{           
    dispatch_queue_t serialdQueue;
    serialdQueue = dispatch_queue_create("update", NULL);
    dispatch_async(serialdQueue, ^{
        [self method1];
    });
    dispatch_async(serialdQueue, ^{
        [self method2];
    });
    dispatch_async(serialdQueue, ^{
        [self method3];
    });
    dispatch_async(serialdQueue, ^{
        [self method4];
    });
    dispatch_async(serialdQueue, ^{
        [self method5];
    });

}

-(void)method1
{        
    //DOWNLOAD JSON FILE
}

-(void)method2 //here i add objects to the core data file
{        
    @try {

        for (NSDictionary *jsonActivity in [json objectForKey:@"Activities"]) {  //ITERATE THROUGH JSON ACTIVITY ARRAY

            NSFetchRequest *request = [[NSFetchRequest alloc] init];

            NSEntityDescription *entity = [NSEntityDescription entityForName:@"Activity" inManagedObjectContext:self.managedObjectContext];
            [request setEntity:entity];

            NSPredicate *searchFilter = [NSPredicate predicateWithFormat:@"title == %@", [jsonActivity objectForKey:@"title"]]; // CHECK IF OBJECT FROM JSON FILE ALREADY EXISTS...
            [request setPredicate:searchFilter];
            NSError *error = nil;

            NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];
            if (error) {
                NSLog(@"Error %@", error);
                abort();
            }

            if ([results count] == 0) { // ADD NEW ACTIVITY IF OLD LIST DOESNT CONTAIN IT

                Activity *activity = [NSEntityDescription insertNewObjectForEntityForName:@"Activity" inManagedObjectContext:self.managedObjectContext];

                activity.title = [jsonActivity objectForKey:@"title"];

                activity.remove = [NSNumber numberWithBool:NO]; // REMOVE FLAG = NO BECAUSE NEW OBJECTS AREN'T REMOVED

            } else  {

                Activity *activity = (Activity*) [results objectAtIndex:0];
                activity.remove = [NSNumber numberWithBool:NO]; // IF OBJECT ALREADY EXISTS IT SHOULD BE OBTAINED
            }

            AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
            [appDelegate saveContext]; // SAVE MO CONTEXT
        }

    }   @catch (NSException *exception) {

        NSLog(@"Exception: %@", exception);
    }
}

-(void)method3 // DELETE OLD OBJECTS 
{   
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Activity" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entityDescription];

    NSPredicate *searchFilter = [NSPredicate predicateWithFormat:@"remove == %@", [NSNumber numberWithBool:YES]];
    [fetchRequest setPredicate:searchFilter];

    NSArray *objectsToDelete = [[NSArray alloc] init];

    NSError *error = nil;
    objectsToDelete = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (error) {
        NSLog(@"Error %@", error);
        abort();
    }

    for (Activity *activity in objectsToDelete) { // DELETE OBJECTS WITH THE PROPERTY REMOVE = YES

        [self.managedObjectContext deleteObject:activity]; // DELETE ACTIVITY
    }

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    [appDelegate saveContext]; // SAVE MO CONTEXT
}

-(void)method4 // CHANGE THE REMOVE PROPERTY TO YES OF ALL OBJECTS
{        
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Activity" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entityDescription];

    NSArray *objects = [[NSArray alloc] init];

    NSError *error = nil;
    objects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (error) {
        NSLog(@"Error %@", error);
        abort();
    }

    for (Activity *activity in objects) {

        activity.remove = [NSNumber numberWithBool:YES];
    }

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    [appDelegate saveContext]; // SAVE MO CONTEXT

    NSLog(@"End Update");
}

-(void)method5 //UPDATE UI
{
    //UI UPDATES
}


推荐答案

您不应该访问 serialQueue 。查看 NSManagedObjectContext 文档

You shouldn't be accessing your managed object context on serialQueue. Take a look at the Concurrency section of the NSManagedObjectContext documentation.

如果在您的代码中您的上下文使用 NSPrivateQueueConcurrencyType NSMainQueueConcurrencyType 并发类型,您可以使用一个基于块的方法确保您在正确的队列:

If in your code your context is using NSPrivateQueueConcurrencyType or NSMainQueueConcurrencyType concurrency type, you can use one of the block-based methods to ensure you're on the right queue:

//asyncrhonous
[self.managedObjectContext performBlock:^{
    //do stuff with the context
}];

//syncrhonous
[self.managedObjectContext performBlockAndWait:^{
    //do stuff with the context
}];

这篇关于核心数据:删除/添加对象时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆