快速同步核心数据和mysql数据库的数据 [英] swift sync data of core data an mysql database

查看:246
本文介绍了快速同步核心数据和mysql数据库的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的swift 2应用程序中,用户可以将数据保存在核心数据中. 将来,我想意识到数据将保存在核心数据和MYSQL数据库中.

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.

在核心数据和mysql之间同步数据的最佳方法是什么

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:

  • 向php文件发出http发布请求,该请求选择mysql数据库的所有数据并将结果返回给应用程序
  • 检查核心数据中存在哪些数据=>无关,哪些是新数据=>将新数据保存在核心数据中

这是最好,最快的方法吗?

is this the best and quickest way to do this ?

推荐答案

没有最快的方法,但是我绝对可以建议一些对我有用的方法.最佳与否-由您决定.

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)向php文件发出http发布请求,该请求选择mysql数据库的所有数据并将结果返回给应用程序-

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 -

收到结果后,您可以像下面的示例一样在Core Data中对其进行解析-

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)检查核心数据中存在哪些数据=>无关紧要,哪些数据是新数据=>将新数据保存在核心数据中-

2) Check which data exist in core data => nothing to do, and which data is new => save the new data in core data -

您可以使用时间戳记进行良好的通话,并检查数据是否已更新. 如果数据已更新-更新单​​个核心数据属性可能会有些棘手.对我一直有效的是删除设备上的SQLITE后端,然后再次运行数据写入方法. 您可以使用下面的示例-

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}
        }

    }

之后,您可以再次运行writeDataToSqlieStore()方法.

After that you can run the writeDataToSqlieStore() method again.

希望有帮助.

这篇关于快速同步核心数据和mysql数据库的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆