首页 > 解决方案 > coredata和sqlite如何同步?

问题描述

我正在制作一个需要单击按钮(true or false alarm)的应用程序,然后将单击哪个按钮、单击日期附加到UITableView. 所有这些信息当前都存储在 coredata 中,我想将 table/coredata 中的数据上传到 sqlite。然后我想将此表上传到数据库,以便它可以用于其他事情。

我试过使用JSONSerialization,但我不确定它是如何工作的。我想json通过按钮视图将文件发送到数据库,但我无法提供UIButton此功能。

对于按钮输入,

@IBAction func addAlarm(_ sender: UIButton){
    let trueFalseAlarm = Alarm(context: moc)
    trueFalseAlarm.added = Date()

    if sender.tag == 0{
        trueFalseAlarm.trueFalse = "0""
    }else if sender.tag == 1{
        trueFalseAlarm.trueFalse = "1"
}

}

表视图:

func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCell(with Identifier: "Cell", for: indexPath)
    let trueFalseAlarm = alarmItems[indexPath.row]
    let trueFalse = trueFalseAlarm.trueFalse
    cell.deatailTextLabel?.text = trueFalse
    let alarmDate = trueFalseAlarm.added as! Date
    let dateFormatter = DateFormatter()
    date Formatter.dateFormat = "d MMMM yyyy, hh:mm"

    cell.detailTextLabel?.text = dateFormatter.string(from: alarmDate)

该表可以工作,但现在问题是在某个时间点获取表的一个实例,并将在 UITableView 中使用的数组发送到数据库中。该数组当前如下所示:

警报项:[(实体:警报;ID:_____;数据:{添加 =“2019-05-24 02:17:51 +0000”;trueFalse = 0;}),...]

tableview 中的数据存储在 coredata 中。是否可以从 coredata 上传/同步数据并将其也包含在 sqlite 中?我需要的只是数据之后的一切。太感谢了。

标签: iosmysqljsonswiftxcode

解决方案


推荐阅读