-1

I'm making an app that has a collection view of plants. I have Plant class that implements the NSCoding protocol so I can save them. The plants are stored in an array called plantList.

var plantList = [Plant]

And here's what I store in my Plant class:

class Plant: NSObject, NSCoding {

var name: String
var species: String
var nextWateredDate: Date
var wateringPeriod: Int
var wateringCount: Int
var profileImage: UIImage?

Whenever a plant is added to the plantList or a plant is edited, I save the plantList like so:

func savePlants() {
    NSKeyedArchiver.archiveRootObject(plantList, toFile: 
    Plant.ArchiveURL.path)
}

My issue is that when I have more than a few plants in my plantList, then savePlants() takes too long to complete. Not sure how to go forward, would a different persistent storage method like core data be faster? Is storing a UIImage in my Plant class too much data?

1 Answers1

0

NSCoding is an object serialization technique. The choice depends on your cache data. If your cache is a bunch of objects, I’d certainly go for NSCoding, as it’s very simple to work with. NSCoding along with NSKeyedArchiver is a great way to store data which is too big for NSUserDefaults but too small and non-numerous for CoreData.

On the other hand, if you want to optimise this code, I'll suggest you to rather save the image name instead of saving the whole image for persistence. Save the image in app's document's directory(of course with the same name) and get fetch it when you are done finishing decoding using the name you decoded. I think it might give you a nice kick in the performance.

Dark Innocence
  • 1,389
  • 9
  • 17