Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
So, I have an app that has a login screen. When I log in in, the app loads a managed object that corresponds to my user profile. But, when I go on to a different screen and trigger a "save" operation, all the data gets stored in the managed object, and the context saves it. However, when I restart the app, the managed object is there, but the data that was just saved is not. I know the object itself is in the store, because the app displays the username at the start of the app.

Code will come after dinner.
 
OK, so here's the code for UserEntity.m:
Code:
-(void)setPlaceIndexPath:(NSIndexPath *)placeIndexPath

{

    NSData *indexPathData = [NSKeyedArchiverarchivedDataWithRootObject:placeIndexPath];

    [self setPrimitiveValue:indexPathData forKey:@"placeIndexPath"];

}

-(NSIndexPath *)placeIndexPath

{

    NSData *indexPathData = [selfprimitiveValueForKey:@"placeIndexPath"];

    if (!indexPathData) {

        // No saved index path.

        return // Omitted. This line is triggered regardless of whether data was entered into the object and a "save" operation occurred.

    }

    else

    {

        NSIndexPath *indexPath = [NSKeyedUnarchiverunarchiveObjectWithData:indexPathData];

        return indexPath;

    }

}
 
IIRC there's some kind of "flush to disk" command and I think it's set as automatic. I don't remember the command off hand. It sounds like it's not actually being written to disk, it might be in a buffer still.

Maybe this helps: http://stackoverflow.com/questions/10428351/when-does-core-data-flush-to-disk

Although, it sounds strange that you can exit and reload an app and not have it saved.

Are you checking the error object to see if it saved without error?
 
IIRC there's some kind of "flush to disk" command and I think it's set as automatic. I don't remember the command off hand. It sounds like it's not actually being written to disk, it might be in a buffer still.

Maybe this helps: http://stackoverflow.com/questions/10428351/when-does-core-data-flush-to-disk

Although, it sounds strange that you can exit and reload an app and not have it saved.

Are you checking the error object to see if it saved without error?

Yes, I checked the error object in my save operation - no errors found.
By the way, the author of the StackOverflow question had nested contexts, which is why his data went forgotten. My application does not use more than one context.

Edit: I did create another thread a few years ago wherein I had assumed my data wasn't saving. The real issue was that I was doing an integer division when I wanted a float.
 
Last edited:
Just ran the app with SQLDebug enabled - I saw a "SELECT" command being passed, but not an "UPDATE" command. Am I supposed to see one if the "save" operation produced no errors?

Edit: My log says:
CoreData: annotation: total fetch execution time: 0.0060s for 1 rows.

So, there's 1 row when there are supposed to be 2 rows. But I have no idea what's going on. This part of the log suggests my app didn't save anything, but - as I said earlier - a different log statement that I programmed suggested that the app did save:

Code:
if (![[[UserEntitysharedEntity] managedObjectContext] save:&error]) {

        NSLog(@"An error occured while trying to save the user's place: %@",[error localizedDescription]);

       

    }

    else

    {

       NSLog(@"%@",@"Successful save!");
    }
 
Last edited:
Where are you setting the attributes of your managed objects? That would be the first place I'd check. Check your attributes of the object right before save context is called
 
Where are you setting the attributes of your managed objects? That would be the first place I'd check. Check your attributes of the object right before save context is called
Normally, I'd agree with you, but in this case, it won't help, because I can see that the data is in there when the app is running.

The app will store the attribute in the managed object, where it will reside only as long as I don't stop the app. The strange thing is, the "save" method that immediately follows the storing of the attribute in question returns a result that seems to indicate a successful save operation.

Edit: The attribute in question is basically an NSObject stored as an NSData object - my subclass had methods that would convert between NSData and an NSObject subclass. However, I just made the attribute a Transformable, so I'll see if that helps.
 
Last edited:
Update: Changing the attribute's type to "Transformable" solved my problem!
Note: Be sure to update the NSManagedObject subclass to reflect the change.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.