Storing NSManagedObjectID vs NSManagedObject

2019-09-04 08:44发布

问题:

I have an app where users can select a specific theme for locations to display. I store the selected theme objectID in NSUserDefaults so that when the app restarts, the selected theme will still be intact.

Throughout the app I reference back to this selected theme. I currently have a class variable to hold the objectID, however I sometimes need to retrieve properties of the object itself (i.e. the name property). To do this I retrive the object from core data and then access my value.

I'm worried about the processing time required to do this, so my question - What is the better option ... ?

1) Store the objectID in memory and query core data to retrieve the object each time I need to access a property. I think this is cheaper in the sense that I have less memory usage, but more expensive in processing.

2) Store the actaul ojbect in memory and then simply access the object whenever I want. I think this is cheaper in the sense of processing, but may be more costly in memory usage.

回答1:

It's not going to matter either way. The amount of time it takes Core Data to retrieve a single object by its ID is going to be effectively 0. The amount of memory used by a single managed object (assuming the object doesn't contain a bunch of image data elements) is going to be negligible.

Base your decision on whatever is easiest to implement and easiest to maintain. Performance is not going to be a factor. I'm currently working on an app that stores hundreds of HTML pages in Core Data and I am constantly reading/writing/modifying these objects with no perceptible lag time whatsoever.