Replace coredata .sqlite file

Hi,


I want to develop a chatting application with local and remote storage. For that, I am using CoreData on front iOS app side and MYSQL database on the server side.
My requirement is that I want to restore the message history at some point in time like when the user updated his/her device to a new one. (The chat history may be large size)
For that, I am planning to work like this:

1. Create a .sqlite zip file from the server. Send it over API and receive and unzip it on the app side.

2. Replace the CoreData .sqlite file with the new one from the server side.


My questions are:
1. Is it possible to update the CoreData .sqlite file with a new one?
2. Is it the right method for this scenario?

1. Is it possible to update the CoreData .sqlite file with a new one?

Yes, but you have to be really careful. Two things:

  • You mustn’t rely on the structure of the Core Data SQLite store. It’s OK to back up and restore it en masse, but if you want to manipulate the internals you must use the Core Data API.

  • Backing up and restoring a Core Data store is trickier than you might think. This was covered in depth in WWDC 2016 Session 242 What’s New in Core Data.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Replace coredata .sqlite file
 
 
Q