【发布时间】: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,它具有可空类型,具有 rotation 和 status
struct DocumentationChanges {
var statusId: String
var rotation: Int
}
【问题讨论】:
-
呃...也许将写入领域代码移动到确认按钮?我不确定我是否完全理解您的问题。
-
您需要跟踪确认操作时的所有旋转,还是在按下确认之前进行最后的旋转??所以我可以帮助你
-
@MuhammadAfzal 是的,所有轮换。您可以按旋转按钮 100 次,它会更改数据库 100 次。但它应该只在你按下确认时改变一次
-
那么你的结构就可以了
-
你说不通。您是说每次用户旋转时数据库都应该更改,而且数据库应该仅在您按下确认后更改。您可能希望将所有执行的旋转存储在一个数组中,然后将它们一次全部写入数据库中,或者......?