Reputation: 1872
I'm fairly new to core data. It's quite confusing.
I want to store many small sized images in Core Data. However, I am confused at the most efficient way of doing this.
When my view controller loads, I fetch from my server a list of images I need to view and load them in a UITableView. In my table view data source I want to check to see if the image is available in Core Data. If not, I load it from the server and then save it to core data.
Here is my problem though...
Is it efficient to make a new core data request for a single image each time 'cellForRowAtIndexPath' is called? Additionally, is it efficient to save a single image to core data when/if I retrieve it from my server?
Or is it better to retrieve all the images from Core Data and then have 'cellForRowAtIndexPath' search for the image it needs in the retrieved images?
Thanks for the help! Trying to understand core data.
Upvotes: 2
Views: 324
Reputation: 119031
It's more efficient to batch fetch the images (or the image paths of the images are large and you store the binary data outside the data store). This is the approach to take to find images for your UI. It's plenty fast to quickly search your in-memory image list to determine if you have the image or need to get it from the server. It isn't so fast to individually fetch from the data store in the same way...
When fetching images from the server it's likely you'll get them individually and then it's appropriate to save them to the data store individually too.
Upvotes: 1
Reputation: 11134
If the images are small (like ~10KB), it should be fine to store them in database.
In my experience, loading images when they are needed leads to much cleaner code base and more memory-efficient and scales better when the number of images increases.
The other approach (loading all images at once) will become slow at some point when the number of images increase.
For mobile database, I recommend using Realm.io instead of Core Data. It's hell a lot easier and less hassle.
Upvotes: 1