FastEasyMapping icon indicating copy to clipboard operation
FastEasyMapping copied to clipboard

Data Fault

Open vishaldeshai opened this issue 7 years ago • 3 comments

Hello,

Why my data fault?? `[MagicalRecord saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { arr = [FEMDeserializer collectionFromRepresentation:[dict objectForKey:@"data"] mapping:[_LeadStatusList defaultMapping] context:localContext]; } completion:^(BOOL contextDidSave, NSError * _Nullable error) { NSLog(@"%@",arr[0]); _LeadStatusList *o = arr[0]; NSLog(@"%hd",o.interactionWith); NSLog(@"%hd",o.outcomeID); NSLog(@"%@",o.orderExpectedDate); NSLog(@"%@",o.nextActionTime); NSLog(@"%@",o.createdTime); NSLog(@"%@",o.reminderTime);

                [_objLead insertObject:o inLeadStatusListAtIndex:0];
                [self.view makeToast:dict[@"message"]];
                [self.navigationController popViewControllerAnimated:YES];
            }];`

Save successfully and and arr contains 1 object that value of all parameters are not there.

thanks in advance.

vishaldeshai avatar Sep 02 '17 13:09 vishaldeshai

Please, read about how nested contexts work. Also you might be interested in reading of the source code of the Magical Record in order to understand how does local saving works. It has nothing to do with the FastEaysMapping itself. Issue lies in your attempt to talk to the objects of the dead context.

dimazen avatar Sep 02 '17 13:09 dimazen

But can you tell me how i can prevent context dead??

vishaldeshai avatar Sep 02 '17 13:09 vishaldeshai

@vishaldeshai MagicalRecord destroys localContext once it is done with the saving. Therefore you need to refetch your objects in the different context (referencing objectID is ok and won't cause fault), or to invoke saving of the context directly and not using a MR's API.

NSManagedObjectContext *context = taken from some api;

[context performBlockAndWait:^{
    array = [FEMDeserializer collectionFromRepresentation:[dict objectForKey:@"data"] mapping:[_LeadStatusList defaultMapping] context:context];
    [context MR_save ]; // choose your MR_save version. I do not remember exact naming
}];
// you're free to use your objects. But again, please pay attention on passing objects across contexts.

dimazen avatar Sep 05 '17 08:09 dimazen