在视图之间保留数据

发布于 2024-11-30 13:49:38 字数 392 浏览 1 评论 0原文

我正在开发一个应用程序,用户在其中输入搜索词并从网络服务接收结果(我控制网络服务)。结果显示在一个合适的视图上,然后用户可以选择一行并导航到三到四级的内部详细信息(我正在使用 uinavigation 控件)。

我想知道在视图之间保留数据的最佳方法是什么。

我目前正在使用应用程序委托来存储一组对象,我可以从应用程序中的任何位置访问这些对象。它工作正常,但我读到这对于这项工作来说不是一个好的做法(我担心内存问题)。我尝试使用 Core Data 框架来完成这项工作,但我意识到我必须读取我的 Web 服务结果,并将它们一一循环,以便将它们插入数据库。我还必须删除旧数据,因为我保存的数据仅适用于当前搜索。我读到了有关 p 列表和将数据保存到文件系统的信息,但找不到适合我工作的真正好的解决方案...... 任何帮助将不胜感激!谢谢!!!

I am developing an app in which the user enter a search word and receives results from a web service (I control the web service). The results are displayed on a uitable view and then the user can select a row and navigate to three - four level of inner details (I am using the uinavigation control).

I am wondering what is the best way to persist the data between the views.

I am currently using the application delegate to store an array of objects which I can access from everywhere in the app. It works fine but I read that it is not a good practice for the job (I am concerned about memory issues) . I tried using Core Data framework for the job but than I realized that I would have to read my web service results, and loop them one by one in order to insert them to database. I also will have to delete old data because the data I am saving is only actual for the current search. I read about p-lists and saving data to file system but could not find a real good solution for my job...
Any help will be appreciated!!! Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

颜漓半夏 2024-12-07 13:49:38

您可以使用 JSON 或 PLISt 进行通信,尽管根据苹果的说法,二进制 plist 在手机端要快得多。

从二进制 Plist 创建字典相对简单:

NSPropertyListFormat format;
NSDictionary *myDictionary = [NSPropertyListSerialization 
        propertyListFromData:data mutabilityOption:NSPropertyListMutableContainers
        format:&format errorDescription:&errorString];

从 JSON 创建字典只需要使用现成的 JSON 库之一。一旦你有了字典,保存它:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES); 
NSString *file = [NSString stringWithFormat: @"%@%@.plist", [paths objectAtIndex:0], @"MyPlistBaseName"];    

[plist writeToFile: file atomically:NO];

然后重新加载它:

NSDictionary *plist = [[[NSDictionary alloc] initWithContentsOfFile: file] autorelease];

但是,如果你将对此数据的访问封装在一个单例中,那么如果速度/内存成为问题,你可以担心优化实现。只需将该signleton视为数据的“所有者”,使用诸如 countgetGroup(0)(返回 25 的块)等方法即可。然后您可以隐藏对象内的所有实现细节。

You can use JSON or PLISt for communication, although binary plist is - according to apple - much faster on the phone side.

Creating a dictionary from the binary Plist is relatively simple:

NSPropertyListFormat format;
NSDictionary *myDictionary = [NSPropertyListSerialization 
        propertyListFromData:data mutabilityOption:NSPropertyListMutableContainers
        format:&format errorDescription:&errorString];

Creating it from a JSON just requires using one of the readily available JSON libraries. Once you have the dictionary, save it:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES); 
NSString *file = [NSString stringWithFormat: @"%@%@.plist", [paths objectAtIndex:0], @"MyPlistBaseName"];    

[plist writeToFile: file atomically:NO];

And re-load it later:

NSDictionary *plist = [[[NSDictionary alloc] initWithContentsOfFile: file] autorelease];

However, If you encapsulate the access to this data in a singleton, then you can worry about optimizing the implementation if speed / memory becomes an issue. Just treat that signleton as the 'owner' of the data, with methods like count, getGroup(0) (to return a block of 25), etc. Then you can hide all the implementation details inside the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文