iOS 中的持久性:属性列表序列化、NSUserDefaults 与核心数据
我是 iOS 新手。我的要求是从应用程序内部收集几个错误字符串并定期将它们发布到我的网络服务。每个错误都是一个简单的 NSString,长度约为 100 个字符。我最多可以在帖子之间累积 1000 条错误记录。实际的发布将在后台线程上完成,但错误日志的收集和持久化将在主线程上完成。我试图在以下持久性方法之间做出决定:
1)属性列表序列化(我的日志记录将是NSStrings的NSArray)
2)NSUserDefaults(我的NSArray将是NSUserDefaults中某些键的值对象)
3)核心数据(但只有一个 NSManagedObject 用于日志,并且其中只有一个属性,即日志消息字符串)
请注意,我说的是“日志”,但我正在手动完成所有日志记录工作。 (还有其他选择吗?)
对我来说一个重要因素是增量地将日志记录保存在磁盘上。换句话说,如果我已经持久化了 500 条日志记录,并且出现了第 501 条日志记录,出于效率原因,我不想重新持久化 500+1 条日志记录。 看来使用选项(1)和(2)我无法增量地保留东西。我想我每次都必须重写该斑点。我还在研究选项(3)。 只是想就此征求专家意见:)
I'm new to iOS. My requirement is to collect several error strings from inside the app and periodically post them to my web service. Each error is a simple NSString ~100 characters long. I may accumulate upto 1000 error records between posts. The actual post will be done on a background thread, but the collection and persistence of the error logs will be done on the main thread. I'm trying to decide between one of the following methods for persistence:
1) Property list serialization (my log records would be NSArray of NSStrings)
2) NSUserDefaults (my NSArray would be the value object for some key in NSUserDefaults)
3) Core Data (But just one NSManagedObject for the log and only one property inside of it, the log message string)
Note that I say "logs" but I'm doing all the logging work manually. (Is there any other option?)
One important factor for me is to incrementally persist the log records on disk. In other words, if I have already persisted 500 log records, and 501st comes along, I do not want to re-persist 500+1 log records for efficiency reasons.
It appears that using option (1) and (2) I cannot incrementally persist stuff. I think I'll have to rewrite the blob everytime. I'm still studying option (3).
Just thought of getting an expert opinion on this :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
* 更新 *
不需要核心数据或类似的东西。
只需在文档目录中创建一个名为 log.txt 的文本文件(实际上什么都可以)。
当您准备好保留 NSArray 时,请使用以下方法:
每当您准备好时,您都可以发布文件,清除其内容,然后为下一个数组做好准备。
我会使用 Core Data,因为它让一切变得如此简单。
另一方面,您实际上不必使用它们中的任何一个。如果我正确地阅读了您的问题,您已经将日志保存在日志文件中。
您可以将lastPost日期存储在NSUserDefaults中,查找在lastPost日期之后创建的日志文件,发布日志文件,更新lastPost日期并完成。
在一个不相关的说明中,发送这样的日志可能会违反应用程序指南,具体取决于您发送的内容以及对用户的清晰程度。
* Update *
No need for Core Data or anything like that.
Just create a text file in your Documents directory call log.txt (anything is fine really).
When you are ready to persist your NSArray, use the following method:
Whenever you are ready you can post the file, clear its contents, and you are ready for the next array.
I would use Core Data just because it makes everything so easy.
On the other hand, you don't really have to use either of them. If I am reading your question correctly you are already persisting the logs in your log files.
You can store a lastPost date in NSUserDefaults, find your log files that were created after the lastPost date, post the log files, update the lastPost date and be done.
On an unrelated note, sending logs like that could violate app guidelines depending on what you are sending and how clear it is to the user.
您还可以使用 NSJSONSerialization,它可以将 JSON 文档格式的数组写入文件,如果您想稍后将数据发送到服务器,这通常很有用。属性列表和 JSON 的规则非常相似,但并不完全相同。
You might also use NSJSONSerialization which can write your array in JSON document format to a file, which is often useful if you want to send the data to a server later. Rules for property list and JSON are quite similar but not identical.