1

I am planning to use Core Data in my app If there is no internet connection.

So what i am doing is retrieving images and videos from my server. I currently download them (Images for example) from the url retrieved from the server and convert it to UIImage NSData and then populate my tableView.

Now to the CoreData:

Some may say storing files in coreData is a bad idea, But as I shall only store a maxium of 5 images/videos at once I don't thing storage or speed shall be a problem as they will not be enourmous files.

To the question:

I have to save I guess the UIImage to CoreData, so obviously I need to get the URL then download the image and finally save it to coreData.

But If I do so in my query for getting the actual posts that would slow down the query possibly a couple of seconds, and I would prefer not to do so. So I was wondering what would be the best way?

I am downloading the images async with SDWebImage each time a cell appears, so If I save the images to coreData once they are downloaded that would mean to save 5 images to CoreData I would need to scroll 5 Cell's.

But I would prefer to: Load the app, download the Images/videos from server as normal and populate the tableView, and once all that has happened I would like the post's to already be saved to coreData.

So If I immediately close the app after the tableView gets populated then disconnect from the internet or go into AirPlane mode and re-open the app I have the posts in the tableView.

I know how to do most of the saving to coreData etc... But I would like some more proffessional advice and tips on how I should approach this.

What I would like to achieve?

1) Open the app

2) Download 5 images/video from the server (Up too here all good)

(From here on I would like some advice)

3) Empty the coreData if there was any previously images/videos saved

4) Save the last 5 images/videos I downloaded to CoreData.

What would be the best solution and quickest way to do so?

Thanks to anyone that can offer some advice.

  • Storing binary files in Core Data is (I believe) doable. However, I'm curious as to why you believe it is necessary to store the files in Core Data as opposed to the filesystem? Your questions seem to relate more to their display in the tableview than anything storage-method related. Storing to filesystem would be pretty simple and would achieve what you list as your requirements. – Anthony C Nov 23 '16 at 18:55
  • @AnthonyC filesystem, not heard of that... I am just wondering what would be the best way to save the data to coreData or filesystem the quickest way...Also removing and adding more if there is already data there... –  Nov 23 '16 at 19:44

1 Answers1

2

Background

iOS devices, like most computers, have persistent storage. (e.g. a hard drive or an ssd)

To persist data across system shutdown events, etc., your app can save it directly to this store using the filesystem. This is a quick and simple way of storing data.

If you need a more structured approach, you could use a database. This approach allows you to work with your data in a more organized fashion (i.e. in rows and columns, with relationships, etc.) This database would be stored on the filesystem, but your app would work directly with the database file(s) itself.

On iOS, the typical database used is SQLite.

Core Data is another layer on top of one (or rarely, multiple) databases. This approach allows you to focus on your data as entities with relationships to each other. (e.g. User -> multiple Follower, etc.) Using Core Data, you can usually ignore the databases and the filesystem underlying it.

On iOS, Core Data is typically implemented with one SQLite database as a store.

Your Use Case

If all you need is to store a few images or videos to your device, then storing directly to the filesystem can work pretty well.

Assuming you already have your file in hand, you just need to:

  • 1 – get a directory location

    var documentDirectoryUrl: URL do { try documentDirectoryUrl = FileManager.default.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) let imageUrl = documentDirectoryUrl.appendingPathComponent("imageFileName.jpg") } catch { // error handling }

  • 2 – write your file to disk

    if (try? imageData.write(to: imageUrl)) == nil { // nil == false print("Error: could not save image to file system.") }

Reference

Anthony C
  • 1,990
  • 3
  • 23
  • 40
  • Thanks for the answer. Well is there any benefit to using filesystem and not coreData? also regarding I already have the file, well I am downloading it but what I download is the url for the image and then download the image from the url when the cell appears. Now I was looking for a way to get the images without displaying the cell and save to core data without taking too much time –  Nov 23 '16 at 22:03
  • There are pros and cons either way. Personally, for just five images, I would store directly to disk because it's quick and easy. (Here are a couple of discussions: [1](http://stackoverflow.com/questions/2090028/core-data-storing-images-iphone), [2](http://stackoverflow.com/questions/4158286/storing-images-in-core-data-or-as-file).) – Anthony C Nov 23 '16 at 22:34
  • As for how to save the image without displaying the cell...that's a coding issue. I'm not sure why you would have difficulty just skipping the code that displays it to cell. Maybe post another question that shows your tableview and image retrieval code. – Anthony C Nov 23 '16 at 22:37
  • WelL I actually have a way at the moment, where I query all the posts from my server and in that same query I am downloading all the images and saving them to coreData but makes query 2-3 seconds instead of being about 1 second...I am trying to fix that issue, if you'd like to see some code of how I am doing it I can show you! I shall look at those discussions now. +1 anyway –  Nov 23 '16 at 22:39
  • 1
    I have started to do it as you suggested, by saving it to disk and linking it in CoreData. I shall share some of my sloution later once I have finished and if you could give me some advice or tips if needed on it i'd appreciate it alot! Accepting anyway! –  Nov 25 '16 at 08:15