dskzap8756 2016-03-03 14:04
浏览 48
已采纳

swift同步核心数据数据和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.

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条回答 默认 最新

  • dongyi8795 2016-03-03 18:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上