我正在创建一个plist来保存一些值,但是在测试过程中,我注意到在应用程序关闭并从多任务处理中删除后,我会保留新创建的plist。但是,如果应用程序被从多任务处理中移除,但如果应用程序被关闭,我就会丢失plist中的值。
这是我的plist控制器类中的save data方法,它管理所有的读/写/保存等操作。
- (void) saveData:(NSString *)methodName signature:(NSString *)pSignature Version:(NSNumber *)pVersion request:(NSNumber *)rNumber dataVersion:(NSNumber *)dvReturned cacheValue:(NSNumber *)cValue;
{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"EngineProperties.plist"];
// set the variables to the values in the text fields
self.signature = pSignature;
self.version = pVersion;
self.request = rNumber;
self.dataVersion = dvReturned;
//do some if statment stuff here to put the cache in the right place or what have you.
if (methodName == @"manu")
{
self.man = cValue;
}
else if (methodName == @"models")
{
self.mod = cValue;
}
else if (methodName == @"subMod")
{
self.sub = cValue;
}
self.cacheValue = [NSDictionary dictionaryWithObjectsAndKeys:
man, @"Manu",
mod, @"Models",
sub, @"SubModels", nil];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjectsAndKeys:
signature, @"Signature",
version, @"Version",
request, @"Request",
dataVersion, @"Data Version",
cacheValue, @"Cache Value", nil];
NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
// check is plistData exists
if(plistData)
{
// write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
NSString *myString = [[NSString alloc] initWithData:plistData encoding:NSUTF8StringEncoding];
// NSLog(@"%@", myString);
}
else
{
NSLog(@"Error in saveData: %@", error);
// [error release];
}
}
@end我的问题有两个部分..我是否可以保存这些值,以便即使应用程序从multitaksing栏中删除时,它们也会保留在plist中。如果它可以工作,我需要做什么更改才能让它工作?
发布于 2012-04-11 11:00:28
这可能与调用saveData方法的位置有关。查看应用程序委托,默认情况下在中有一些存根。您可能需要applicationWillResignActive、applicationDidEnterBackground或applicationWillTerminate。请查看每种方法的文档,您需要的方法将取决于您希望写入数据的时间。
https://stackoverflow.com/questions/10099192
复制相似问题