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.

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

报告相同问题?

悬赏问题

  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。