There is no quickest way but I can definitely suggest something that works for me. Best or not - its up to you to decide.
Also your question is very generic - as it encompasses the entire backend infrastructure of a mobile app. I'll try to be as specific as I can -
1) Make an http post request to a php file, which select all data of the mysql database and give the result back to the app -
Once you receive the result, you can Parse it in Core Data like the example below -
func writeDataToSqliteStore() throws {
// Configure the Network Call here with NSURL & NSData returning an array that you can write to the Core Data SQLITE backend.
do {
jsonResponse = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as? NSArray
} catch {
print(error)
}
let localEntity = self.insertNewEntity("EntityName") as? CoreDataEntityClass
for singleObject in jsonResponse {
localEntity.name = singleObject.valueForKey("name")
localEntity.address = singleObject.valueForKey("address")
}
}
2) Check which data exist in core data => nothing to do, and which data is new => save the new data in core data -
You can do good ol head call with a Time Stamp and check if the data has been updated.
If the data has been updated - it could get a little tricky updating individual Core Data attributes. What has always worked for me is deleting the SQLITE backend on the device and running the data write method again.
You can use the example below -
func removeSQLITEFiles() {
let fileManager = NSFileManager.defaultManager()
let URL1 = self.applicationDocumentsDirectory.URLByAppendingPathComponent("File.sqlite")
let URL2 = self.applicationDocumentsDirectory.URLByAppendingPathComponent("File.sqlite-wal")
let URL3 = self.applicationDocumentsDirectory.URLByAppendingPathComponent("File.sqlite-shm")
if fileManager.fileExistsAtPath(URL1.path!) {
do {
try fileManager.removeItemAtURL(URL1)
}
catch {error}
}
if fileManager.fileExistsAtPath(URL2.path!) {
do {
try fileManager.removeItemAtURL(URL2)
}
catch {error}
}
if fileManager.fileExistsAtPath(URL3.path!) {
do {
try fileManager.removeItemAtURL(URL3)
}
catch {error}
}
}
After that you can run the writeDataToSqlieStore()
method again.
Hope that helps.