plist saving worked in ios3.2 but not in 4.1

Discussion in 'iOS Programming' started by AbhishekApple, Nov 12, 2010.

  1. AbhishekApple macrumors member

    Joined:
    Aug 5, 2010
    #1
    I am trying to save edited Array back to the plist... this code worked fine in ios3.2 in simulator...

    Now in ios4.1 (while debugging) when i click the exit button on simulator (and not the Task red button on xcode) it saves to plist and again when i click
    the app icon on simulator, it shows the saved changes...........but when i terminate the app by clicking the Task button and restart the app, it doesn't shows the changes....

    i have the following code....

    Code:
    
    -(void)applicationWillTerminate:(UIApplication *)application{
    called save function
    }
    - (void)applicationDidEnterBackground:(UIApplication *)application{
    called save function
    }
    
    + (void)save
    {
    	//save contacts list
    	NSString *path = [[self class] pathForDocumentWithName:@"Contacts.plist"];
    	NSString *plist = [listOfTempContacts description];
    	
    	NSError *error = nil;
    	[plist writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    	
    	if (error)
    	{
    		NSLog(@"Error writing file at path: %@; error was %@", path, error);
    	}
    	
    	//save favourite contacts list
    	path=nil;
    	plist=nil;
    	path = [[self class] pathForDocumentWithName:@"FavContacts.plist"];
    	plist = [listOfTempFavContacts description];
    	
    	error = nil;
    	[plist writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    	if (error)
    	{
    		NSLog(@"Error writing file at path: %@; error was %@", path, error);
    	}
    	
    }	
    

    Thank you for reading
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #3

    Code:
    +(void)initialize{
    
    	if (listOfContacts == nil)
        {
            NSString *path = [[self class] pathForDocumentWithName:@"Contacts.plist"];
    		 //NSMutableArray *listOfTempContacts = [NSMutableArray arrayWithContentsOfFile:path];
            
            if (listOfTempContacts == nil)
            {
                NSLog(@"Unable to read plist file at path: %@", path);
                path = [[NSBundle mainBundle] pathForResource:@"Contacts"
                                                       ofType:@"plist"];
                
            }
    		listOfTempContacts=[[NSMutableArray alloc] initWithContentsOfFile:path];
    		//Initialize the merge and sectioned array
    		listOfContacts = [[NSMutableArray alloc] init];
    		[self showContacts];
    	}
    	if (listOfFavContacts == nil)
        {
            NSString *favpath = [[self class] pathForDocumentWithName:@"FavContacts.plist"];
    		//NSMutableArray *listOfTempContacts = [NSMutableArray arrayWithContentsOfFile:path];
            
            if (listOfTempFavContacts == nil)
            {
                NSLog(@"Unable to read plist file at path: %@", favpath);
                favpath = [[NSBundle mainBundle] pathForResource:@"FavContacts"
                                                       ofType:@"plist"];
               
            }
    		 listOfTempFavContacts = [[NSMutableArray alloc] initWithContentsOfFile:favpath];
    		//Initialize the merge and sectioned array
    		listOfFavContacts = [[NSMutableArray alloc] init];
    		[self showFavContacts];
    	}
    	if (listOfSortContacts==nil) {
    		listOfSortContacts=[[NSMutableArray alloc]init];
    	}
    	if (listOfSortFavContacts==nil) {
    		listOfSortFavContacts=[[NSMutableArray alloc]init];
    	}
    }
    
    + (NSString *)pathForDocumentWithName:(NSString *)documentName
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, 
    														 YES);
    	
        NSString *path = [paths objectAtIndex:0];
        
        return [path stringByAppendingPathComponent:documentName];
    }
    
    

    from the console
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  5. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #5
    Thank you, you asked this question....
    Now it is working fine by commenting the orange code...

    Code:
    if (listOfFavContacts == nil)
        {
            NSString *favpath = [[self class] pathForDocumentWithName:@"FavContacts.plist"];
    		//NSMutableArray *listOfTempContacts = [NSMutableArray arrayWithContentsOfFile:path];
            
    		
           [COLOR="DarkOrange"] if (listOfTempFavContacts == nil)
            {
                NSLog(@"Unable to read plist file at path: %@", favpath);
                favpath = [[NSBundle mainBundle] pathForResource:@"FavContacts" ofType:@"plist"];
               
            } [/COLOR]
    		          NSLog(@"NEw File at path: %@", favpath);
    		 listOfTempFavContacts = [[NSMutableArray alloc] initWithContentsOfFile:favpath];
    		//Initialize the merge and sectioned array
    		listOfFavContacts = [[NSMutableArray alloc] init];
    		[self showFavContacts];
    	}
    
    The below code searched the file first in the main bundle directory where it finds one coz of which it showed the contents of existing file and not the edited file
    Code:
    [[NSBundle mainBundle] pathForResource:@"FavContacts" ofType:@"plist"];
     
  6. cnstoll, Nov 12, 2010
    Last edited by a moderator: Nov 12, 2010

    cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #6
    For what it's worth, I believe the method you're using to write to file is actually deprecated after iOS 3.2. The only thing I use writeToFile: for now is for blanking out a file :) Check out NSFileHandle and it's writeData method.

    EDIT:
    Actually, maybe it's only deprecated for NSString. I just remembered I use it for writing out to a plist, but my data for the plist is an instance of NSData. Try and convert your data to NSData (should be pretty easy) and change the receiver of writeToFile to be the NSData instead of the NSString. The section of my app where I use NSFileHandle instead of writeToFile involved strings, so that's probably why I switched to using that instead in that case. So you can either switch to NSFileHandle and writeData, or convert your plist data into a NSData object and try again.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    From method documentation for writeToFile:atomically: from the Class Reference for NSString:
    So, it's not like you shouldn't use writeToFile: on an NSString. Just don't do so using the simplest version.
     

Share This Page