简体   繁体   English

将iPhone应用程序数据存储在低内存中

[英]Storing iphone application data on low memory

I have some data structures in my app that I need to persist upon receiving 'didReceiveMemoryWarning' notification. 我的应用程序中有一些数据结构,在接收到“ didReceiveMemoryWarning”通知后,需要保留这些数据结构。 The data is kind of a running log of all of the actions the user has performed with the app (which is a game) 数据是用户使用该应用(游戏)执行的所有操作的运行日志。

The data I have may not be a small amount (possible > few hundred KB) so plists don't seem to be the right solution. 我所拥有的数据可能不是一个小数目(可能>几百KB),因此使用pl似乎不是正确的解决方案。

The first of two possibilities is archiving objects and making these objects support NSCoding protocol. 两种可能性中的第一种是归档对象,并使这些对象支持NSCoding协议。 I'm not sure if this is the right path to choose. 我不确定这是否是正确的选择。

The second option seems to be with CoreData, using NSManagedObjectModel and NSPersistentStoreCoordinator. 第二个选项似乎与CoreData一起使用,使用NSManagedObjectModel和NSPersistentStoreCoordinator。 Is this a good way to store these objects? 这是存储这些对象的好方法吗? Or is it overkill? 还是过度杀伤力? (I'm using the 'Recipes' sample app from Apple for reference). (我正在使用Apple提供的“食谱”示例应用程序作为参考)。

My objects are custom object types which eventually hold NSString, NSNumber, NSInteger and other simple types. 我的对象是自定义对象类型,它们最终包含NSString,NSNumber,NSInteger和其他简单类型。

Sample of some of the data types I have: 我有一些数据类型的样本:

// this the base object I need to start with to persist
@interface MyDataObject : NSObject
{
    MyScore        *aScore;
    // Contains an object of type 'MyAction'
    NSMutableArray *allActions; 
}

@interface MyScore : NSObject
{
    NSInteger  currentScore;
    NSDate     lastUpdated;
}

@interface MyAction
{
    NSNumber   *actionId;
    NSString   *description
    MyUser     *associatedUser;
}
@interface MyUser
{
    NSNumber *id;
    NSString *name;
    NSString *email;
}

User can play a bunch of different games and for each game, I have an activity log of what moves they've made. 用户可以玩很多不同的游戏,对于每个游戏,我都有一个活动日志,记录了他们所做的动作。 The user can see the moves they've made so far in each game while they're playing it and they can also switch between active & inactive games so they can review the past moves as well. 用户在玩游戏时可以看到他们到目前为止在每个游戏中所做的动作,他们还可以在活动和不活动的游戏之间切换,以便他们也可以查看过去的动作。

A warning, here. 警告,在这里。 If your app starts getting these messages, and you're using the handler to write out huge gobs of data, the kernel may not let your app finish saving stuff if the situation is dire (from the kernel's POV). 如果您的应用程序开始收到这些消息,并且您正在使用处理程序写出大量数据,那么如果情况很糟(从内核的POV出发),内核可能不会让您的应用程序完成保存工作。 Whatever approach you use with your log, you should be dripping this data to the backing store gradually, so you can be confident that you won't lose any data if this situation occurs. 无论您使用哪种日志记录方法,都应逐渐将这些数据滴到后备存储中,这样您就可以确信,在这种情况下不会丢失任何数据。

I'd suggest several things. 我会建议几件事。

  1. How much of the data is actually being used for anything right now? 目前有多少数据实际用于任何事物? If there's a good chance it's not being used then save it. 如果很有可能没有使用它,请保存它。

  2. How much can be recreated/reconstructed? 可以重新创建/重建多少?

Take a look at the SQLite book example provided by Apple. 看一下Apple提供的SQLite书籍示例。

I am working on an app which creates gobs of data along the way. 我正在开发一个可沿途创建数据块的应用程序。 Most is not used, but I have no idea which data will be used. 大多数没有使用,但我不知道将使用哪些数据。 What I do is keep a small cache of the data most likely to be used and the rest goes to the SQLite database in realtime. 我要做的是为最可能使用的数据保留一个小的缓存,其余的将实时存储到SQLite数据库中。 My memory requirements stay very small, 100K or so. 我的内存需求保持很小,大约100K。 In the past it was megs (and crashes). 过去是兆次(并崩溃)。

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

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