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

Posted

技术标签:

【中文标题】目录在 iOS 应用程序启动之间不持久【英文标题】:Directory not persisting between iOS app launches 【发布时间】:2012-06-11 03:53:52 【问题描述】:

所以我试图在我正在制作的应用程序的打开之间存储一些数据,但由于某种原因,我正在制作的目录和我正在存储的数据在应用程序打开之间没有保留,我可以不知道为什么……

我有三种方法:

- (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"]];

当我打印出在pathForDataFile 中创建目录的结果时,每次它都会打印 1,这意味着每次它都在创建该目录,即使它不应该能够,因为该目录应该已经存在一个之后称呼。在applicationWillTerminate 中调用saveDataToDisk,在applicationDidBecomeActive 中调用loadDataFromDisk。任何人都可以帮助我了解为什么会这样吗?

【问题讨论】:

applicationWillTerminate 可能没有给您足够的时间。您可以从其他地方调用该方法进行测试吗? @WarifAkhandRishi 事情是虽然它正在打印出NSLog()s,所以这意味着该方法正在被及时调用,这不是时间问题...... 【参考方案1】:

您需要在代码中进行一些额外的错误检查,以尝试缩小问题实际发生的范围。

错误检查类似于原始示例代码中的新行:

- (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 );

【讨论】:

我使用了这些错误检查,这些行就是输出的内容(每个方法输出一个错误,错误按照上面方法的顺序)was not able to load data from file at /var/mobile/Applications/B2D28246-F274-4789-9976-178EF6F649CC/Library/MyAppwas not able to write data to path /var/mobile/Applications/B2D28246-F274-4789-9976-178EF6F649CC/Library/MyApp@987654325 @ 编辑:抱歉格式错误,我知道如何解决这个问题...... 所以看起来你在“saveDataToDisk”中的“NSKeyedArchiver”调用失败了。我的猜测是您的“usedPlaces”属性中的对象之一不是可以轻松存档的对象。您需要确保该数组/字典中的 everything 可以保存到磁盘(即符合 NSCoding 协议)。 Check out this Apple article on encoding & decoding objects.

以上是关于目录在 iOS 应用程序启动之间不持久的主要内容,如果未能解决你的问题,请参考以下文章

核心数据在启动之间不持久

iOS 持久缓存目录

仅套接字 IO 会话不持久

在K8S中使用Local持久卷

代号一中的存储持久性

iOS 数据持久化