swift sync data of core data an mysql database [cl

2020-08-04 09:53发布

问题:

in my swift 2 app the user can save data in core data. in the future, i would like to realize that the data will save in core data AND in a MYSQL Database.

what is the best way to sync data between core data and mysql

my idea was, that on each app start a process will start:

  • 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
  • check which data exist in core data => nothing to do, and which data is new => save the new data in core data

is this the best and quickest way to do this ?

回答1:

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.