在 iOS 上保存大数组的最佳方法

Best method to save large array on iOS

我编写了一个与 BLE 设备交互的 iOS 应用程序 - BLE 设备发送 iOS 设备数据,应用程序分析数据然后保存。 iOS 应用程序大约每秒接收一次数据,因此我保存读数的数组(如 NSString)会很快变大。

该应用程序可以在后台运行,到目前为止我一直在使用 NSUserDefaults 来保存这个大数组。我在我的应用程序上进行了跟踪,发现它在后台使用了 iPhone 6 的 CPU 的 3%,并发现是 NSUserDefaults 造成的。我仔细阅读了它,发现 NSUserDefaults 为此目的效率低下。

现在,我想放弃这种方法并使用不同的方法。我读过一些这样的方法,比如将数据保存到 CoreData、Plists 或纯文本文件中。这些方法的实施效率和易用性如何?我以前用一个文本文件做过一些事情,这真的很容易,但很多时候我需要把那个文本文件的全部内容加载到一个数组中进行解析,这似乎对内存有问题。所以,如果您有任何建议,我很乐意听取。

查看 Apple Performance Tips,我发现了您的问题。

简而言之:在写入磁盘之前获取大量数据,以尽量减少使用 Core Data 或 SQLite 对闪存驱动器的访问。

您可以在 this link

中查看如何实现 SQLite 持久化

改进文件管理 - 性能提示

Minimize the amount of data you write to the disk. File operations are relatively slow and involve writing to the flash drive, which has a limited lifespan. Some specific tips to help you minimize file-related operations include:

  • If your data consists of structured content that is randomly accessed, store it in a Core Data persistent store or a SQLite database, especially if the amount of data you are manipulating could grow to more than a few megabytes.