Updating core data database
If you are using Swift 3, you are probably going to find errors in your project if you miss the last step pointed by the blue arrow, just change the module to “Current Product Module” and the Codegen to “Manual/None” (I found this solution in Stack Overflow)Finally, in editor click on Create NSManaged Object Subclass, if you want I add a Model group in the project where you can save this new class (just to keep this organized but you don’t have to)Now, you have your class and if you go inside the file you will see that it don’t look like the ones that you are used to.When I am saying that we are going to create a Photo object you are probably thinking in create it by subclassing NSObject but what we need instead is an object subclass of NSManaged Object, this is a generic class that implements all the basic behavior required of a Core Data model object, its not that hard to implement so just follow along with me, start by opening the file with extension .xcdatamodeld in your bundle (it is where all your files are), click on it, it will open something like this.The red arrow points to the section where you need to add a name for your entity (entity is just a name for an object subclass of NSmanagedobject), type Photo.Here we are using the URLSession datatask with completion method to make a GET request to the server, inside it, we are using guard statements to handle potential errors, then we are using the JSONSerialization class to parse the data and downcast it into a dictionary.
Finally, you can see that we call the completion on the main thread, that’s because closures are executed Asynchronously, remember that, if not, your app can crash. Go to Photo VC and add this inside view Didload and run your app.
We are ready now for the second part of this post, Core Data.
Updating core data database comments