菜鸟教程小白 发表于 2022-12-12 18:32:55

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


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

<p>应用程序已启用在后台运行,直到此时我一直在使用 <code>NSUserDefaults</code> 来保存这个大数组。我对我的应用程序进行了跟踪,发现它在后台使用了 iPhone 6 的 CPU 的 3%,并发现 NSUserDefaults 导致了这种情况。我阅读了它,发现 <code>NSUserDefaults</code> 为此目的是多么低效。</p>

<p>现在,我想放弃这种方法并使用不同的方法。我读过一些这样的方法,比如将数据保存到 CoreData、Plists 或纯文本文件中。这些方法的效率和实现难度如何?我之前用文本文件做过一些事情,这很容易,但我经常需要获取该文本文件的全部内容并将其加载到数组中进行解析,这似乎对内存有问题。因此,如果您有任何建议,我很乐意听取他们的意见。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>查看 <a href="https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTips/PerformanceTips.html" rel="noreferrer noopener nofollow">Apple Performance Tips</a> ,我发现了你的问题。</p>

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

<p>您可以在 <a href="http://www.appcoda.com/sqlite-database-ios-app-tutorial/" rel="noreferrer noopener nofollow">this link</a> 中查看如何实现 SQLite 持久性。 </p>

<hr/>

<p><strong>改进文件管理</strong> - 性能提示</p>

<blockquote>
<p>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:</p>

<ul>
<li>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.</li>
</ul>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 iOS 上保存大数组的最佳方法,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35368119/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35368119/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 iOS 上保存大数组的最佳方法