【问题标题】:File's AccessDate not altered after reading the File by [NSData dataWithContentsOfURL:...]通过 [NSData dataWithContentsOfURL:...] 读取文件后,文件的 AccessDate 未更改
【发布时间】:2012-08-22 07:49:54
【问题描述】:

我正在文件系统中缓存一些用户信息。然后使用

阅读
NSData *cachedMessagesData = [NSData dataWithContentsOfURL:fileDirectoryForUserInfoCache];

我需要知道每个缓存文件的访问时间,但是,当我使用 Property 键检查访问时间时:

NSArray *filesInUserInfoCacheDirectory = [self.fileManager contentsOfDirectoryAtURL:self.cacheDirectoryForUserInfo includingPropertiesForKeys:[NSArray arrayWithObjects:@"NSURLCreationDateKey", @"NSURLContentAccessDateKey", nil] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];

NSDate *accessDate = [[[filesInUserInfoCacheDirectory lastObject] resourceValuesForKeys:[NSArray arrayWithObject:@"NSURLContentAccessDateKey"] error:nil] objectForKey:@"NSURLContentAccessDateKey"];

我发现访问日期与文件创建日期相同。通过 [NSData dataWithContentOfURL:...] 读取文件不会更改文件的访问时间属性。

谁能告诉我为什么?有没有办法读取文件以更新访问时间?非常感谢提前。

【问题讨论】:

    标签: nsurl


    【解决方案1】:

    回答我自己的问题。我终于不得不工作了一点。 NSURL 文件的访问日期属性在读取文件后无法更新,它的日期始终与 NSURL 文件的创建日期相同。一种解决方法是每次读取后覆盖文件,然后更新访问日期。但这对我来说听起来很奇怪。

    另一种方法是使用 NSURL 文件的修改日期属性。这个属性可以通过 FileManager 的 API setAttributes: ofItemAtPath: 来设置。每次读取文件后,我都设置了修改日期属性,而不是实际修改文件。然后稍后通过 NSURL API resourceValuesForKeys: 读回这个 NSDate 对象。

    [self.fileManager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[fileDirectoryForUserInfoCache path] error:nil]; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多