Reputation: 2067
I wrote an iOS application that interfaces with BLE devices - the BLE device sends the iOS device data, and the application analyzes the data and then saves it. The iOS app receives data about every second, so the array in which I'm saving my readings (as NSString
) gets large very quickly.
The app is enabled to work in the background, and up until this point I've been using NSUserDefaults
to save this large array. I did a trace on my app and saw that it was using 3% of an iPhone 6's CPU in the background, and found out that NSUserDefaults was causing this. I read up on it and saw how inefficient NSUserDefaults
is for this purpose.
Now, I would like to transition away from this method and use something different. I've read about a few of such methods, like saving data to CoreData, in Plists, or in just a plain text file. How efficient and easy to implement are such methods? I've done things with a text file before, which was really easy, but quite often I need to take the entire contents of that text file and load it into an array for parsing, which seems like it would be problematic for memory. So, if you have any suggestions I would love to hear them.
Upvotes: 2
Views: 647
Reputation: 2317
Looking in to the Apple Performance Tips, I found something to your problem.
In short: Get an significant amount of data before writing in disk to minimize the access to the flash drive using Core Data or SQLite.
You can check how to implement an SQLite persistence in this link
Improve Your File Management - Performance Tips
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.
Upvotes: 2