简体   繁体   English

iPhone NSUserDefaults持久性难度

[英]iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults . 在我的应用程序中,我有一堆数据存储在NSUserdefaults This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). 该信息包括一个带有NSStringsNSNumbersNSObject (Object1),以及另一个对象(Object2)的2个实例。 After alot of searching and trying i got it to correctly save (and load) this data through the use of NSCoding and NSKeydArchiver . 经过大量搜索和尝试后,我通过使用NSCodingNSKeydArchiver使其正确保存(和加载)了这些数据。

Every screen in my app basically is a view of a little piece of information from this datamodel. 我的应用程序中的每个屏幕基本上都是该数据模型中的一小部分信息的视图。 Since one can change data in some of these views the data needs to be saved and loaded between screen transitions. 由于可以在其中一些视图中更改数据,因此需要在屏幕转换之间保存和加载数据。

My screen view chain is this: root -> view 1 -> subview 1 我的屏幕视图链是这样的:根->视图1->子视图1

Here comes the odd part: 奇怪的是:

In subview 1 i load the dataobject from the NSUserdefaults and present the information (this works), then the user can edit the data (in this case a NSString describing a company name) When the user presses the back button in the topbar to return to "view 1" it triggers a save (this also works). 在子视图1中,我从NSUserdefaults加载数据NSUserdefaults并显示信息(此方法有效),然后用户可以编辑数据(在本例中为描述公司名称的NSString )。当用户按下NSUserdefaults的“后退”按钮以返回到“视图1”会触发保存(这也有效)。

Back at "View 1" a viewWillAppear event is triggered. 回到“视图1”,触发一个viewWillAppear事件。 In this trigger I reload the saved data from "subview 1" and redraw my screen (a UITableViewController ). 在此触发器中,我从“子视图1”重新加载了保存的数据,并重画了我的屏幕( UITableViewController )。 For some reason, after the load the changes made in the previous screen are not reflected! 由于某些原因,加载后不会反映在前一个屏幕中所做的更改! The loaded object still contains the same values as if the save hadn't happened! 加载的对象仍然包含与未发生保存相同的值!

After some logging it seems that even though i CALL the save before the LOAD, it apparently gets executed later. 经过一些日志记录后,即使我在LOAD之前调用了保存,它显然也会在以后执行。 So the confirmed (by debug breakpoints) actions are -> LOAD -> SAVE -> LOAD. 因此,确认的(通过调试断点)操作是-> LOAD-> SAVE-> LOAD。 But according to the logfunction (i log each save and load call) it ends up as -> LOAD -> LOAD -> SAVE. 但是根据logfunction(我记录每个保存和加载调用),它最终显示为-> LOAD-> LOAD-> SAVE。

Can anyone help me with this? 谁能帮我这个? - I have no idea what i'm doing wrong here. -我不知道我在做什么错。

This code is called from an object i create wherever i need it (in time it needs to be replaced with a singleton) and i access the saved and loaded data by MyDataStoreObject.currentData 从我需要的地方创建的对象调用此代码(及时将其替换为单例),然后通过MyDataStoreObject.currentData访问保存和加载的数据。

- (void) saveCurrentData {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject: currentData];
    [defaults removeObjectForKey:@"MYAPP_CURRENTDATA"];
    [defaults setObject:data forKey:@"MYAPP_CURRENTDATA"];

    [defaults synchronize];
}

- (void) loadCurrentData {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSData *data = [defaults objectForKey:@"MYAPP_CURRENTDATA"];

    if (data == nil)
    {
        currentData = nil;
        NSLog(@"NO DATA TO RETRIEVE FROM USERDEFAULTS");
    }
    else
    {
        currentData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    }
}

Call synchronize on your NSUserdefaults instance after you save, to ensure all the contents you save are written down and preserved immediately. 呼叫同步你的NSUserdefaults实例保存后,以确保所有您保存都写下来,并立即保存的内容。

Edit: 编辑:

In

When the user presses the back button in the topbar to return to "view 1" it triggers a save (this also works). 当用户按下顶部栏中的“后退”按钮以返回到“视图1”时,它将触发保存(这也有效)。

How did you do that? 你是怎么做到的? Show us the code that catches the back button being touched, I'm curious if it performs the save every time. 向我们展示捕获被触摸的后退按钮的代码,我很好奇它是否每次都执行保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM