1
votes

I have a relatively simple entity. When I create it, set its attributes, and save it, it saves successfully. I can later retrieve it and it is not nil and I get a successful save message from MagicalRecord.

When I retrieve it and try to access any attribute though the attribute is nil. The entity itself is fine but the attributes are all nil. I have checked they are all definitely set correctly before I save.

I haven't encountered this problem before. Why could it be occurring?

NB: This doesn't happen every time. Most times I call the method to create and save this entity it can later be retrieved without any issues. The problem is intermittent but possible to replicate on every run.

Code:

    Entity1 *entity1 = [Entity1 MR_createEntityInContext:localContext];
    [entity1 setUpEntity:myobject];

    EntityChild *entityChild=[EntityChild MR_createEntityInContext:localContext];
    [entityChild setUpEntityChild:entity.child withContext:localContext];
    [entityChild setEntity1:entity1];

    [localContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {

    }];

Update:

If I look in the sqlite database and search for the entity it actually doesn't exist at all. So MagicalRecord tells me it saves, CoreData lets me retrieve a non-nil object (albeit with nil attributes) but no record exists in the database.

2
show your code please - iCode
@iCode I added the code that creates and saves the entity. Do you need any other code? - user470763
Why do you say it tells you it saves... your completion block is empty... are you sure there is no error? Also, that's not enough code... the details are likely important, and you didn't include any of them. - Jody Hagins

2 Answers

1
votes

I did not understand ur code standards. As I am new to IOS Development. I Used below code for retrieving.

   NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
   NSEntityDescription *entityRef = [NSEntityDescription entityForName:@"Entity1" inManagedObjectContext:appDelegate.managedObjectContext];//localContext
   [fetchRequest setEntity:entityRef];
   NSError *error=nil;
   NSArray *detailsArray = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];

   if (error) {
   NSLog(@"Unable to execute fetch request.");
   NSLog(@"%@, %@", error, error.localizedDescription);
  }

Saving the data

NSManagedObjectContext *context = [appDelegate managedObjectContext];//localContext
NSManagedObject *objectRef = [NSEntityDescription
                                           insertNewObjectForEntityForName:@"Entity1"                                          inManagedObjectContext:context];
 [objectRef setValue:@"IOS" forKey:@"Name"];
 [objectRef setValue:@"positive" forKey:@"Attitude"];
 NSError *error;
                if (![context save:&error]) {
                    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
                }

Hope it helps you...!

0
votes

Ok, I got to the bottom of this. It wasn't a problem with the code when I did the save. It was actually a problem with some code in another class that was retrieving the data from the wrong context. When I changed the context it worked correctly.

I'm still not sure why this only happened occasionally and not every time the code was run but it's working now.

Thanks for your help anyway everyone.