将对象添加到核心数据的速度太慢

时间:2013-04-16 06:24:18

标签: iphone core-data

每次,当我保存NSManagedObjectContext时,它会 10秒或更多。

这是我的代码:

dispatch_queue_t requestQueue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
        dispatch_async(requestQueue, ^{
        NSManagedObjectContext *manageContext = [[NSManagedObjectContext alloc]init];
        [manageContext setPersistentStoreCoordinator:[[OKCoreManager sharedManager] persistentStoreCoordinator]];
        Card *cardObject = (Card *)[NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:manageContext];
        cardObject.name = self.name;
        cardObject.title = self.title;
        cardObject.email = self.email;
        cardObject.address = self.address;
        cardObject.stacks = self.stackObject;
        cardObject.contact =self.contact;
        NSError *error;
        if (![[OKCoreManager sharedManager]saveManagedObjectContext:manageContext])
        {
             NSLog(@"Problem saving: %@", [error localizedDescription]);
             dispatch_sync(requestQueue, ^{
                     iCallBack(FALSE,selfObject);
             });
        }
        else
        {
             dispatch_sync(requestQueue, ^{
                      iCallBack(TRUE,selfObject);
             });
        }
        manageContext = nil;
});

1 个答案:

答案 0 :(得分:0)

自己排序:

dispatch_queue_t requestQueue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
    dispatch_async(requestQueue, ^{

        NSManagedObjectContext *manageContext = [[NSManagedObjectContext alloc]init];

        [manageContext setPersistentStoreCoordinator:[[OKCoreManager sharedManager] persistentStoreCoordinator]];

         Card *cardObject = (Card *)[NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:manageContext];

        cardObject.name = self.name;
        cardObject.title = self.title;
        cardObject.email = self.email;
        cardObject.address = self.address;
        cardObject.stacks = self.stackObject;
        cardObject.contact =self.contact;

    BOOL value = [[OKCoreManager sharedManager]saveManagedObjectContext:manageContext];
    dispatch_sync(dispatch_get_main_queue(), ^{
                iCallBack(value);
            });
            manageContext = nil;

    });