Difficulties writing data to a plist on program termination

Discussion in 'iOS Programming' started by cnstoll, Oct 4, 2010.

  1. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #1
    So my goal is to write out my app's user data to a plist when the app terminates...so I went about implementing this by following an Apple tutorial... I get the feeling that this tutorial is not entirely accurate though, so I'm coming here for help.

    http://tuvix.apple.com/library/ios/...rtyLists/QuickStartPlist/QuickStartPlist.html

    Basically everything works great in terms of reading in from the plist in the main bundle. No issues there. But when I go to save the plist on termination in the ~/Documents folder...I get problems. Basically it doesn't create the file, and I'm not sure why. I can look in the App's ~/Documents on the simulator and it's not there, and on a device I can't actually look in the folder, but from the behavior I can tell it's not working either.

    Here's the code (getWeightDictionary returns my "test" data in dictionary format with the correct keys/values):

    Code:
    - (void)applicationDidEnterBackground:(UIApplication *)application {
    	NSString *error;
    	NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    	NSString *plistPath = [rootPath stringByAppendingFormat:@"Weight.plist"];
    	NSDictionary *plistDict = [weightDataController getWeightDictionary];
    	NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
    	
    	if (plistData) {
    		[plistData writeToFile:plistPath atomically:YES];
    	}
    	else {
    		NSLog(error);
    		[error release];
    	}
    }
    Any Ideas? I changed the method from ApplicationShouldTerminate to the entersBackground one...because I can't find any definition that ApplicationShouldTerminate (mentioned in the tutorial) even exists. My guess is it's a Mac OS X method.
     
  2. cnstoll thread starter macrumors 6502

    Joined:
    Aug 29, 2010
    #2
    Also, for what it's worth, I tried creating an empty plist inside the mainbundle and writing out to that as well (in the same enterBackground method) and that didn't work either. I know the method got called though, because I can trace the execution through the whole thing when I exit the app.
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    What is NSPropertyListSerialization and why do you think you need to use it? NSDictionary has a perfectly sensible writeToFile:atomically: method.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Use stringByAppendingPathComponent when building paths.

    Print out your path to see that it's valid.

    Check the error returned from writeToFile.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    The correct method name under iOS is applicationWillTerminate:.
     
  6. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #6
    ...Unless, of course, the App supports background execution, in which case applicationDidEnterBackground: is correct :D
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    ...as an additional delegate method to be concerned with, yes. :D
     
  8. cnstoll thread starter macrumors 6502

    Joined:
    Aug 29, 2010
    #8
    hehe, that was it...

    It was actually writing the file, it was just calling it .../Applications/blah/DocumentsWeight.plist :p

    thanks
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    You should use stringByAppendingPathComponent: instead of stringByAppendingFormat when building your path.
     
  10. cnstoll thread starter macrumors 6502

    Joined:
    Aug 29, 2010
    #10
    Yeah I changed that and that solved the problem. I believe that's what I'd intended to use in the first place but auto complete filled in the wrong method an I didn't notice :/
     
  11. laputafish macrumors newbie

    Joined:
    Oct 7, 2010
    #11
    I just had same problem two days ago.
    Maybe I just typed and saw "stringByAppendi", pressed <Enter> and expected it was what I want. I overlooked the name given. :D

    Nothing is written to file but no error.

    I solved it only after using NSLog to output value of each variable one by one.
    :p
     
  12. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #12
    Also make sure that it does not take longer then 10 seconds to write out the file. If it does it will terminate with a crash log on the device.
     
  13. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #13
    I use this code when writing plist files

    Code:
    BOOL written = [d writeToFile:path atomically:NO];
    DebugLog(written ? @"Successfully wrote plist file to %@":@"Couldn't write plist file to %@", path);
    
    When you're on the Sim you can almost never be certain what folder your app is in so having the path in the console allows me to find the plist file easily if I need to.
     

Share This Page