Swift - Saving to Core Data
<p>I am planning to use Core Data in my app If there is no internet connection.</p>

<p>So what i am doing is retrieving <strong>images</strong> and <strong>videos</strong> 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.</p>

<p>Now to the CoreData:</p>

<p>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.</p>

<p><strong>To the question:</strong></p>

<p>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.</p>

<p>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. <strong>So I was wondering what would be the best way?</strong> </p>

<p>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.</p>

<p>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. </p>

<p><strong>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.</strong></p>

<p>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.</p>

<p><strong>What I would like to achieve?</strong></p>

<p>1) Open the app </p>

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

<p>(From here on I would like some advice)</p>

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

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

<p>What would be the best solution and quickest way to do so?</p>

<p>Thanks to anyone that can offer some advice.</p>