【问题标题】:How can I write all changes in the database once the user confirms it?用户确认后如何在数据库中写入所有更改?
【发布时间】:2020-02-03 11:42:14
【问题描述】:

我制作了一个菜单,用户可以在其中访问多个按钮来旋转更改文档状态。每次用户选择按钮时,更改都会写入领域数据库


//changes the status of the dc
case "changeStatus" :
            if let marker = documentationMarkers.first(where: {$0.documentationId == doc.id}) {
                let nav = UINavigationController()
                let ctrl = StatusPickerViewController()
                ctrl.delegate = self
                nav.pushViewController(ctrl, animated: true)
                nav.modalPresentationStyle = .popover
                self.present(nav, animated: true)
                nav.popoverPresentationController?.permittedArrowDirections = .any
                nav.popoverPresentationController?.sourceView = marker
                nav.popoverPresentationController?.sourceRect = marker.bounds
            }

//rotates the document
case "rotateMarker" :
            if let marker = documentationMarkers.first(where: {$0.documentationId == doc.id}) {
                marker.rotate()
                let documentation = documentations.first(where: {$0.id == marker.documentationId})

                var rotation = documentation?.rotation.value ?? 0

                rotation = (rotation + 15) % 360

                _ = self.updateRotation(documentation: documentation!, rotationAngle: rotation)
            }

updateRotation 方法在存储扩展中,并将更改写入数据库:

func updateRotation(documentation: Documentation, rotationAngle: Int) throws {
        if let realm = try? Realm() {
            try realm.write {
                documentation.updated = Date()
                documentation.rotation.value = rotationAngle
            }

            return
        }

        throw MobiplanError.invalidStateError(message: "Opening the database failed")
    }

我的确认按钮目前是空的,因为我不知道如何实现它,以便在按下时将更改提交到数据库中,而不是每次选择另一个按钮时。

所以 f.e.当您按旋转按钮 100 次时,它会更改数据库100 次。当您按确认时,它应该只更改一次。

我想到了一个 struct,它具有可空类型,具有 rotationstatus

struct DocumentationChanges {
    var statusId: String
    var rotation: Int
}

【问题讨论】:

  • 呃...也许将写入领域代码移动到确认按钮?我不确定我是否完全理解您的问题。
  • 您需要跟踪确认操作时的所有旋转,还是在按下确认之前进行最后的旋转??所以我可以帮助你
  • @MuhammadAfzal 是的,所有轮换。您可以按旋转按钮 100 次,它会更改数据库 100 次。但它应该只在你按下确认时改变一次
  • 那么你的结构就可以了
  • 你说不通。您是说每次用户旋转时数据库都应该更改,而且数据库应该仅在您按下确认后更改。您可能希望将所有执行的旋转存储在一个数组中,然后将它们一次全部写入数据库中,或者......?

标签: ios swift database realm


【解决方案1】:

请记住,您当前的代码不会向您的 Realm 数据库写入任何内容,您的结构也没有任何可为空的类型。

为了实现您想要的,您可以创建一个DocumentationChanges 数组,并且每次用户按下旋转按钮时,将新的旋转保存在数组中。

当用户单击确认按钮时,将数组保存在您的 Realm 数据库中。

这样可以保存您的旋转:

var rotationStory = [DocumentationChanges]()
func updateRotation(documentation: Documentation, rotationAngle: Int) {
    let documentationChange = DocumentationChanges(statusId, rotationAngle)
    rotationStory.append(documentationChange)
}

然后将所有执行的旋转存储在数据库中,一旦单击确认按钮:

func storeToRealm() {
    do {
        if let database = try? Realm()
        try database.write {
           database.add(rotationStory)
        }
    } catch {
        throw
    }
}

【讨论】:

  • _ = self.updateRotation(documentation: documentation!, rotationAngle: rotation) 在我的代码中调用上述 func 并将更改写入数据库(我认为是这样。 ..)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
  • 2023-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多