I do know that MagicalRecord
will execute saveWithBlock
on backend thread and execute completion
on main thread, but just get confused about how to pass entity in saveWithBlock to completion block, specifically:
Event *wantToCreateEvent = nil;
Event *wantToUpdateEvent = toBeUpdatedEvent;
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext){
wantToCreateEvent = [Event MR_createEntityInContext:localContext];
Event *localContextEvent = [wantToUpdateEvent MR_inContext:localContext];
localContextEvent.attri = @"newValue"
} completion:^(BOOL success, NSError *error) {
// Can I use wantToCreateEvent directly here?
// Is wantToUpdateEvent updated here?
}
For Entity Creation you can use 2 functions of Megical Record
+ (id) MR_createEntityInContext:(NSManagedObjectContext *)context;
+ (id) MR_createEntity;
Note: For Saving the Entities You just have to Save the Context in which these entities are created
Save In Background
+ (void) saveWithBlock:(void(^)(NSManagedObjectContext *localContext))block;
+ (void) saveWithBlock:(void(^)(NSManagedObjectContext *localContext))block completion:(MRSaveCompletionHandler)completion;
Save in Main Thread
+ (void) saveWithBlockAndWait:(void(^)(NSManagedObjectContext *localContext))block;
For More Understanding about CoreData With MegicalRecord I would recommend you to go through this tutorial