0

每次,当我保存我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;
});
4

1 回答 1

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;

    });
于 2013-04-16T07:49:32.450 回答