I read the answers to the question asked by TomF about … How to update an @ObservedRealmObject in a SwiftUI view. My question is quite similar except for the fact that the field value is drawn from an Enum. The object of the exercise is to change the value in the field(itemStatus) by clicking on the field.
I have run this basic code as a standalone view and it works without problems leading me to believe the problem is caused by my use of the ObservedRealmObject.
I am hoping that someone will be able to show me how to update the value of itemStatus in the View and in the DB(.realm).
Here is the View…
import SwiftUI
import RealmSwift
struct InspItemStatusView: View {
@ObservedObject var rlmMgr = RealmManager()
@ObservedRealmObject var item:InspItem
var body: some View {
VStack(alignment:.leading) {
var newStatus:ItemStatusEnum = .na
Button {
if item.itemStatus.index < ItemStatusEnum.allCases.count - 1 {
//item.itemStatus =
newStatus = ItemStatusEnum.allCases[item.itemStatus.index + 1]
} else {
//item.itemStatus =
newStatus = ItemStatusEnum.allCases[0]
}
updateItemStatus(newValue: newStatus.rawValue)
} label: {
Text(item.thaw()!.itemStatus.rawValue)
}
.minimumScaleFactor(0.5)
.buttonStyle(.plain)
.fontWeight(.semibold)
.foregroundColor(
item.itemStatus.rawValue == "N/A" ? .secondary :item.itemStatus.rawValue == "Rec" ? .orange : item.itemStatus.rawValue == "IP" ?.red : .green)
}//Vstack
//}
}
func updateItemStatus(newValue:String){
do {
try Realm().write() {
guard let thawedItem = item.thaw() else {
print("Unable to thaw item")
return
}
print("ItemStatus Update hit")
// thawedItem.itemStatus = newValue) ?? ItemStatusEnum.na
}
} catch {
print("Failed to save Item: \(error.localizedDescription)")
}
}
}
Based on the previous example (TomF), I applied the thaw to the item and it worked as evidenced by the execution of the print statement; “ItemStatus Update hit”
The InspItem object and Enum…
import Foundation
import RealmSwift
public enum ItemStatusEnum: String, PersistableEnum, CaseIterable{
case na = "N/A"
case recorded = "Rec"
case ip = "IP"
case closed = "Closed"
var index: Int {
ItemStatusEnum.allCases.firstIndex(where: {$0 == self}) ?? 1
}
}
public enum ItemTypeEnum: String, PersistableEnum, CaseIterable {
case O = "Observation"
case D = "Deficiency"
case R = "Recommendation"
}
final class InspItem : Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted(indexed: true) var inspId:Int = 0
@Persisted(indexed: true) var itemId:Int = 0
@Persisted var itemType : String = "" //O,D,R
// @Persisted var itemStatus : String = "Recorded" //"IP", "Closed" //<==First Try
@Persisted var itemStatus : ItemStatusEnum = ItemStatusEnum.recorded //"IP", "Closed"
@Persisted var itemDescript : String = ""
@Persisted var photos = RealmSwift.List<Photo>()
@Persisted(originProperty: "items") var inspection:LinkingObjects<Inspection> //backlink
//convenience init(inspId:Int ,itemId:Int, iType:String, iStat:String, iDescript:String) {
convenience init(inspId:Int ,itemId:Int, iType:String, iStat:ItemStatusEnum, iDescript:String) {
self.init()
self.inspId = inspId
self.itemId = itemId
self.itemType = iType
self.itemStatus = iStat
self.itemDescript = iDescript
}
}
At this point, I am not completely sure on how to update the value of itemStatus. I presume that the code that I have will save and display the updated value, but if not, please show me what to do.
Thanks
KenT