gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:40 24 4
gpt4 key购买 nike

我编写了一个与 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.

关于ios - 在 iOS 上保存大数组的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35368119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com