首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保留plist但丢失plist值

保留plist但丢失plist值
EN

Stack Overflow用户
提问于 2012-04-11 10:52:07
回答 1查看 111关注 0票数 0

我正在创建一个plist来保存一些值,但是在测试过程中,我注意到在应用程序关闭并从多任务处理中删除后,我会保留新创建的plist。但是,如果应用程序被从多任务处理中移除,但如果应用程序被关闭,我就会丢失plist中的值。

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

代码语言:javascript
复制
- (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中。如果它可以工作,我需要做什么更改才能让它工作?

EN

回答 1

Stack Overflow用户

发布于 2012-04-11 11:00:28

这可能与调用saveData方法的位置有关。查看应用程序委托,默认情况下在中有一些存根。您可能需要applicationWillResignActive、applicationDidEnterBackground或applicationWillTerminate。请查看每种方法的文档,您需要的方法将取决于您希望写入数据的时间。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10099192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档