Data persistence not working in iTahDoole


#1

Hi all,

So after following the code in the book - iTahDoodle is complete and running on my test device = however, once I quite the application and relaunch it on the device - data I had entered using the UI is now gone?

My test device is an iPhone 4 with iOS 6 on it.

If my understanding is correct - this bit of code is what is used to preserve the data, correct?

- (void)applicationDidEnterBackground:(UIApplication *)application { [tasks writeToFile:docPath() atomically:YES]; }

I also put it in:

- (void)applicationWillTerminate:(UIApplication *)application { [tasks writeToFile:docPath() atomically:YES]; }

However adding / removing these bits of code has zero effect on my app?


#2

Could you paste the code for your [color=#008000]NSString *docPath()[/color] function?


#3

Sure.

BNRAppDelegate.h

[code]#import <UIKit/UIKit.h>

NSString *docPath(void);

@interface BNRAppDelegate : UIResponder <UIApplicationDelegate, UITableViewDataSource>[/code]

BNRAppdelegate.m

[code]#import “BNRAppDelegate.h”

NSString *docPath()
{
NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

return [[pathList objectAtIndex:0]stringByAppendingPathComponent:@"data.td"]; 

}
[/code]


#4

OK, well that looks fine.

At the top of you [color=#008000]application:didFinishLaunchingWithOptions[/color]: method do you have:

NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()];
if (plist)
{
    tasks = [plist mutableCopy];
}
else 
{
    tasks = [[NSMutableArray alloc] init];
}

which should load the contents of the file (if it exists)?

Nick


#5

[quote=“fujilla”]OK, well that looks fine.

At the top of you [color=#008000]application:didFinishLaunchingWithOptions[/color]: method do you have:


// Attempt to load an existing to-do dataset from an array stored to disk.
NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()];
if (plist)
{
// If there was a dataset available, copy it into our instance variable.
    tasks = [plist mutableCopy];
}
else 
{
// Otherwise, just create an empty one to get us started.
    tasks = [[NSMutableArray alloc] init];
}

which should load the contents of the file (if it exists)?

Nick[/quote]

I’ve added the comments to the code if that clears it up?

Thanks for the help.


#6

Tander,

I am not quite sure what you are saying by simply quoting something I said and adding comments to it.

Are you saying you have the exact same code in your project?

If so, I am not sure what your problem is, all I can suggest is that you copy/paste all of the code you have.

Nick


#7

Hi Nick,

Sorry for not being clear.

The code is identical to the book - I’ve even copied it.

I will revisit the chapter after I have finished the book to try find the error.

Thanks for the help though.

Regards,
Rob.


#8

I had this issue. After some debugging I found that the write to the file was failing. The path provided by “NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)” did not exist for me - “/Users/$USER/Library/Application Support/iPhone Simulator/7.1/Applications/[big crazy number]/Library/Documentation/” was provided. That directory existed up to, but not including, the “Documentation” folder at the end.

The code did’t include any error handling…so that failure was not obvious when executing, and the app behaved exactly the same with and without the persistence code. Once discovering the write failure (“error code 4”), I created the Documentation folder, and then everything worked perfectly - the save down to data.td worked and the persistence worked after that. I would have thought the writeToFile: would have created the folder “Documentation” folder…apparently not.


#9

I had the same problem. In my case it was because I was quitting the application by using Product/Stop from Xcode. This just shuts down the application without allowing it to run the applicationDidEnterBAckground or applicationWillTerminate callbacks.

When I use the Home button in the iOS simulator (Command Shift H) the file gets correctly written. And when I relaunch the application it loads the previous tasks.