Coredata performBlock then return the new value

天涯浪子 提交于 2019-12-01 08:16:10

问题


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.


回答1:


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);
        }
    }];
}



回答2:


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.



来源:https://stackoverflow.com/questions/33563578/coredata-performblock-then-return-the-new-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!