Resolved Core Data app "forgets" managed object attribute after being stopped

Discussion in 'iOS Programming' started by moonman239, Aug 3, 2015.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    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.
     
  2. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #2
    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;
    
        }
    
    }
    
     
  3. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #3
    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?
     
  4. moonman239, Aug 4, 2015
    Last edited: Aug 4, 2015

    moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #4
    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.
     
  5. moonman239, Aug 4, 2015
    Last edited: Aug 4, 2015

    moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #5
    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:
    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!");
        }
    
       
     
  6. Punkjumper macrumors member

    Joined:
    Jan 12, 2013
    #6
    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
     
  7. moonman239, Aug 4, 2015
    Last edited: Aug 4, 2015

    moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #7
    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.
     
  8. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #8
    Update: Changing the attribute's type to "Transformable" solved my problem!
    Note: Be sure to update the NSManagedObject subclass to reflect the change.
     

Share This Page