开发者

problems saving to persistent storage iphone

I am writing an iphone app; I need to save the data to persistent storage, but I am having the problems... I created an entity in xcdatamodel and I have no problem getting the default value set for my attribute. In my testing, I go thru the code several times below and can change this attribute (it's an integer) while the application is active. However, when I close and reopen the app, my integer (myInt) is back to the default value when I expected it to be the last value I updated it to. Here is what I am doing:

NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSEntityDescription *myTestEntity = [NSEntityDescription
                                       entityForName:@"TestEntity"
                                       inManagedObjectContext:managedObjectContext];

NSFetc开发者_如何学ChRequest *fetchRequest = [[NSFetchRequest alloc] init];

[fetchRequest setEntity:timeStampEntity];

NSError *error = nil;
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
    // TODO: Handle the error
}
for (id oneObject in fetchedObjects)
{
    NSInteger myInt = [[oneObject valueForKey:@"myStoredInt"]intValue];
    myInt++;
    [oneObject setValue:[NSNumber numberWithInt: myInt] forKey:@"myStoredInt"];
    NSError *error = nil;
    if (![managedObjectContext save:&error]) {//TODO: Handle error      
    } 
}

[fetchRequest release];

I was hoping that line:

if (![managedObjectContext save:&error])

would do the job and store the changes to the persistent storage, but it does not! (Having this line inside or outside the for loop does not change the result) What am I doing wrong?

Thank you very much for your help!


It will be helpful if you could show your app delegate code. My guess is that somewhere you are reseting the data your self. maybe in the method that you load the start data.

3 notes for now:

  1. you don't need to place the save method inside the loop. you should place it after all the changes are made and just once.
  2. check if you have a save method called in the (if not add them):

        - (void)applicationWillTerminate:(UIApplication*)application {
           [self saveContext];
         } 
    
        - (void)applicationDidEnterBackground:(UIApplication*)application {
         [self saveContext];
        }
    
  3. if it dose not help try to see if you are loading the default values on every lunch of the app.

GOOD LUCK

and welcome

EDIT

Hey, you can do that -

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }

    NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]                                             stringByAppendingPathComponent: @"anyname.sqlite"]];

   //check if the store already exist?
    BOOL firstRun = NO; 
    if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path] isDirectory:NULL]) {
        firstRun = YES;     
    }

    NSError *error = nil;
    persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    if(firstRun){
        //add your default data here
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜