简体   繁体   English

在iOS应用启动之间目录不持久

[英]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.

相关问题 核心数据不会在应用启动之间保留数据 - Core Data is not persisting data between app launches 使用应用程序组在应用程序之间通信和持久化数据 - Communicating and persisting data between apps with App Groups 如何在iOS应用启动后立即显示实时摄像机预览? - How to show live camera preview as soon as iOS app launches? 在iPhone应用程序重新启动之间未保存sqlite数据 - sqlite data is not saved between iPhone app re to launches 如何在应用程序启动之间保留CLLocation? - How do I persist CLLocation between app launches? 当移动网络浏览器使用自定义URL启动本机iOS应用程序时传递什么元数据? - What metadata is passed when a mobile web browser launches a native iOS app using a custom URL? 在iOS 8中启动应用程序时,如果设备处于横向,则如何强制纵向显示 - How to force portrait orientation if device is in landscape orientation when the app launches in iOS 8 Google的“ Google搜索2.0”应用如何在iO上启动其他应用? - how does Google's “Google search 2.0” app launches other apps on iOs? 尝试在iOS7应用启动时收集用户信息,并使用该信息填充主视图 - Trying to collect user info when iOS7 app launches and use that info to populate the main view 3启动应用后显示UIAlertview - Show UIAlertview after 3 launches App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM