Coredata performBlock then return the new value

2019-02-19 14:49发布

How to return the new object saved in coredata if I am using performBlock to save a managedObjectContext?

The requirement is, add an entry in coredata, and return it. My code is something like this:

//create a privateMOC
NSManagedObjectContext *private = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];

//set parentContext
[private setParentContext:self.coredataManager.managedObjectContext];

__block Detail *object = nil;

[private performBlock:^{
    //fetch from the db
    object = [self.coredataManager insertObjectWithEntityName:NSStringFromClass([Detail class])];

    //save the private context
    NSError *error = nil;
    if (![private save:&error]) {
        NSLog(@"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]);
    }

}];

return object;

Here since the saving is happening in Block, the return will execute first. But I need the object with the latest value. Any suggestion please.

2条回答
Emotional °昔
2楼-- · 2019-02-19 15:27

Rather than return the newly created Core Data object, pass the constructor method a block with a parameter of the type of object you're creating.

When the object has been created within the Managed Object Context's performBlock: block, call your completion block and pass in the newly constructed Core Data object.

+ (void)coreDataObjectWithJSON:(NSDictionary *)json completion:(void (^)(NSYourCoreDataObject *coreDataObject))completion {

    [yourManagedObjectContext performBlock:^{

        NSEntityDescription *entity = [NSEntityDescription entityForName:... inManagedObjectContext:...];

        NSYourCoreDataObject *coreDataObject = [[NSYourCoreDataObject alloc] initWithEntity:entity insertIntoManagedObjectContext:...];

        if (completion) {
            // "return" the new managed object
            completion(coreDataObject);
        }
    }];
}
查看更多
Luminary・发光体
3楼-- · 2019-02-19 15:27

I did some research and found that was not too difficult to fix this issue:

I replaced performBlock with performBlockAndWait and it worked.

Reason is straightforward: performBlock is asynchronous, while performBlockAndWait is synchronous.

查看更多
登录 后发表回答