保留plist但失去plist值

时间:2012-04-11 02:52:07

标签: iphone ios plist

我正在创建一个plist来保存一些值,但是在测试期间我注意到我在应用程序关闭后保留了我新创建的plist&从多任务中删除。但是,如果应用程序从多任务处理中删除,我会在该plist中丢失我的值,但如果该应用程序已关闭则不会... ...

这是我的plist控制器类中的保存数据方法,它管理所有的读/写/保存等。

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

我的问题有两个部分.. 我是否可以保存这些值,以便即使应用程序从多重锐化条中移除也会保留在plist中。 如果它可以工作,我需要改变什么才能使它工作?

1 个答案:

答案 0 :(得分:0)

这可能与调用saveData方法的位置有关。查看应用程序委托,默认情况下有几个存根。您可能需要applicationWillResignActive,applicationDidEnterBackground或applicationWillTerminate。检查每个文档,您想要的文档将取决于您希望何时写入数据。