我目前正在构建一个相当大的 iPhone 应用程序。反正比我预期的要大。但这不是重点,该应用程序的总体思想是从 Web 服务中获取 JSON,将其全部分类为自定义 NSObject,这些 NSObject 链接在一起然后呈现。
这一切都很好。但是,因为我希望用户能够在没有互联网连接的情况下在设备上看到这些信息,所以我需要将我呈现的信息保存到每个应用程序拥有的 Documents 文件夹中。
我基本上将 NSCoding 协议实现到所有需要它的自定义 NSObject 中,以便将其保存到 Documents 目录的子目录中。
这一切都是通过这里的这个功能来实现的。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Data"];
NSString *dataFileString = [dataPath stringByAppendingPathComponent:@"Company.archive"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataFileString]) //Does directory already exist?
{
MACompany *company = [MACompany sharedMACompany];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Data"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) //Does directory already exist?
{
if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(@"Create directory error: %@", error);
}
}
NSString *dataFileString = [dataPath stringByAppendingPathComponent:@"Company.archive"];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:company forKey:@"MACompany"];
[archiver finishEncoding];
[[NSFileManager defaultManager] createFileAtPath:dataFileString
contents:data
attributes:nil];
[archiver release];
[data release];
} else {
NSLog(@"File already exists, no need to recreate, not yet anyway");
}
}
application didFinishLaunchingWithOptions:
当用户首次加载应用程序 ( ) 和用户在后台打开应用程序 () 时,我执行以下请求applicationWillEnterForeground:
。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Data"];
NSString *dataFileString = [dataPath stringByAppendingPathComponent:@"Company.archive"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataFileString]) //Does directory already exist?
{
NSLog(@"Create a new Company Request");
MAWebRequests *companyReq = [[MAWebRequests alloc] init];
[companyReq getCompanyDetails];
[companyReq release];
} else {
NSLog(@"Saved Company Needs to be Decoded applicationWillEnterForeground");
MACompany *company = [MACompany sharedMACompany];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Data"];
NSString *dataFileString = [dataPath stringByAppendingPathComponent:@"Company.archive"];
NSData *data = [[NSData alloc] initWithContentsOfFile:dataFileString];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[data release];
company = [unarchiver decodeObjectForKey:@"MACompany"];
[unarchiver finishDecoding];
[unarchiver release];
}
现在这一切都很好,我也可以从这个文件中提取。但是,当我将 Xcode 的调试器附加到应用程序时,我只能获取存储在此文件中的数据。一旦停止,数据就会损坏并且不包括原始数据。
数据仍然存储在那里,我可以看到创建的文件,但是存储在文件中的实际数据本身是错误的......
我不应该使用上述逻辑将数据保存到文件然后重新创建共享对象吗?有没有其他人试图做这样的事情并取得了成功?我遇到这个奇怪的问题有什么原因吗?其他人遇到过这个问题吗?
任何帮助将不胜感激,一直在尝试各种不同的方法来让它工作,但没有任何东西能够到达那里。我需要做的就是能够将数据永久存储在那里,直到我需要更新它......