So I'm trying to store some data between openings of an app I'm making, but for some reason the directory I'm making and the data I'm storing aren't persisting between app openings, and I can't figure out why…
I have three methods:
- (NSString *)pathForDataFile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"MyApp"];
NSLog(@"%@", documentsDirectory);
NSError *error;
NSLog(@"%i", (int)[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error]);
return documentsDirectory;
}
- (void)saveDataToDisk
{
NSString * path = [self pathForDataFile];
NSMutableDictionary * rootObject;
rootObject = [NSMutableDictionary dictionary];
[rootObject setValue: [self usedPlaces] forKey:@"key"];
[NSKeyedArchiver archiveRootObject: rootObject toFile: path];
}
- (void) loadDataFromDisk
{
NSString * path = [self pathForDataFile];
NSDictionary * rootObject;
rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
[self setObject: [rootObject valueForKey:@"key"]];
}
When I print out the result of creating the directory in pathForDataFile
, every time it prints 1, which means every time it's creating that directory, even though it shouldn't be able to since the directory should already exist after one call. saveDataToDisk
is called in applicationWillTerminate
, and loadDataFromDisk
is called in applicationDidBecomeActive
. Can anybody help me as to why this is happening?