Jack Jack - 13 days ago 4
Swift Question

Swift - Saving to Core Data

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.

Answer

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