Resolved Archiving works in simulator, not on phone

Discussion in 'iOS Programming' started by MickeyT, Mar 9, 2013.

  1. MickeyT, Mar 9, 2013
    Last edited: Mar 9, 2013

    MickeyT macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #1
    I have copied the following word-for-word from a book to obtain a save path for archiving using NSCoding:


    Code:
    -(NSString *)itemArchivePath {
        
        NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        
        NSString *documentDirectory = [documentDirectories objectAtIndex:0];
        
        return [documentDirectory stringByAppendingPathExtension:@"projects.archive"];
                 
    }
    This is followed by calling this method:

    Code:
    -(BOOL)saveChanges {
        
        NSString *path = [self itemArchivePath];
        
        return [NSKeyedArchiver archiveRootObject:dataBaseArray toFile:path];
        
    }
    This successfully saves when it is run in the simulator (when I reopen the app after closing it fully, all objects that were previously in the array dataBaseArray reappear). When I run the app on the iPhone, it doesn't work.

    I think it is maybe something to do with the path that is obtained on the iPhone. I changed the itemArchivePath method to the following to see if that worked:

    Code:
    -(NSString *)itemArchivePath {
        
        NSString *documentDirectory = [[NSBundle mainBundle]bundlePath];
        
        return [documentDirectory stringByAppendingPathExtension:@"projects.archive"];
                 
    }
    This again works in the simulator but not on the iPhone.

    For information, the screenshot shows the path that is obtained when I step through the code whilst running on the iPhone.

    Does anyone know why this works in the simulator but not on the device?

    Thank you.
     

    Attached Files:

  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    What do you mean by it doesn't work?

    You should use stringByAppendingPathComponent.

    You can't write to the app bundle.
     
  3. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #3
    So the contents of the dataBaseArray populates a table view. When I close the app in the simulator, double-tap the home button, kill the app, and relaunch it, the table view repopulates with all the information that was there previously.

    When I do the same exercise on the iPhone, it doesn't repopulate when I reopen it.

    The following method calls the saveChanges:

    Code:
    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        
        BOOL success = [[DataStore database] saveChanges];
        
        if (success) {
            
            NSLog(@"Save successful!");
            
        } else {
            
            NSLog(@"Save failed");
            
        }
        
    }
    When running in the simulator, "Save successful" is logged to the console, but when running on the iPhone "Save failed" is logged.

    Thank you so much, because this has worked!

    And I have been a stupid idiot as well, because, on rechecking the book, I should have typed stringByAppendingPathComponent - I have fallen foul of the autocomplete and didn't realise it.

    Why, when running in the simulator, did stringByAppendingPathExtension still work?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Because the Simulator runs under OS X, it runs with less restrictions. Another example is file name case sensitivity.
     
  5. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #5
    Thanks.

    So is the bundle essentially what I see in the navigator in Xcode, and it is out of bounds for being written to by the app when it is running?

    I ask because I have created plist files before and added them into the navigator and accessed them from the bundle. Obviously, in this case, the archive is being written by the app rather than by me manually, so is that where the difference lies?

    Edit: it can't be this because I have also changed those plist files via the app as well, so I just don't get it.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Try writing that plist back to the bundle and see what happens...
     
  7. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #7
    Okay...got it!

    Thank you both for your help.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    MickeyT, can I ask you what resources you are using to learn the fundamentals of iOS Programming? Try to be as specific as possible.
     
  9. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #9
    I'm using the book you recommended to me in a post a while ago - iOS Programming: Big Nerd Ranch Guide 3rd Edition, plus the developer documentation.

    To be honest, I have jumped ahead of the book: I am up to chapter 11, which is about navigation controllers. But I was so desperate to have a go at something myself I quickly read the chapter on subclassing table view cells and a chapter called "Saving, Loading and Application States" (so I could save things) and I'm now having a go at something on my own and practicing what I've read in chapters 1 to 11.

    I think my mistake here was mistyping something the book said, and my questions on the bundle probably come from not having got to the part of the book yet which explains this.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Good choice.

    Resist this temptation. This book is laid out in a very logical progression. I would suggest going through the entire thing before really trying to "make mistakes". For example, you've jumped ahead to Chapter 14: "Saving, Loading, and Applications States". What is covered here might not make complete sense without having gone through all the previous chapters. Oh, and by the way, the end of that chapter has a "For The More Curious: The Application Bundle" section. Guess which questions it would've answered?

    So, slow down. Take it easy. Concentrate on the current chapter. And make sure you are very comfortable with what it covered before moving on to the next.
     
  11. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #11
    Okay, I'll park what I'm doing and come back to it. For what its worth, I did manage to animate the drawn circle in chapter 6 to bounce round the screen on my own! But alas, perhaps at the moment I've taken a step too far in doing my own thing just now!

    Thank you.
     

Share This Page