[英]Directory not persisting between iOS app launches
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. 当我在pathForDataFile
打印出在目录中创建目录的结果时,每次打印1时,这意味着它每次都在创建该目录,即使它不应该这样做,因为一次调用后该目录应该已经存在。 saveDataToDisk
is called in applicationWillTerminate
, and loadDataFromDisk
is called in applicationDidBecomeActive
. saveDataToDisk
被称为applicationWillTerminate
,并loadDataFromDisk
被称为applicationDidBecomeActive
。 Can anybody help me as to why this is happening? 有人可以帮我了解为什么会这样吗?
You need to do some additional error checking in your code to try to narrow down where the problem is actually happening. 您需要在代码中执行一些其他错误检查,以尝试缩小问题实际发生的位置。
Error checking like the new lines in your original sample code: 像原始示例代码中的新行一样进行错误检查:
- (NSString *)pathForDataFile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"MyApp"];
BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error];
// success is NO only if there is an error. success is YES if directory was created or if the directory already exists
if(success == NO)
{
NSLog( @"error creating directory at path %@ is %@", documentsDirectory, [error localizedDescription] );
}
return documentsDirectory;
}
- (void)saveDataToDisk
{
NSString * path = [self pathForDataFile];
if(([self usedPlaces] == NULL) || ([self usedPlaces count] == 0))
{
NSLog( @"usedPlaces is either null or has no entries");
}
NSDictionary * rootObject = [NSDictionary dictionaryWithObject: [self usedPlaces] forKey: @"key"];
BOOL success = [NSKeyedArchiver archiveRootObject: rootObject toFile: path];
if(success == NO)
{
NSLog( @"was not able to write data to path %@", path );
}
}
- (void) loadDataFromDisk
{
NSString * path = [self pathForDataFile];
NSDictionary * rootObject;
rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if(rootObject)
[self setObject: [rootObject valueForKey:@"key"]];
else
NSLog( @"was not able to load data from file at %@", path );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.