0
我有一個函數,我通過一個payment
和一個debt
對象(都是代表我的Realm數據庫中的數據的結構)。然後,我想將payment
附加到debt
的paymentHistory
。下面是我用這個代碼:境界不堅持寫入的變化
static func savePayment(payment: Payment, forDebt debt: Debt) throws {
let realm = try Realm()
let predicate = NSPredicate(format: "created = %@", debt.created)
guard let dbDebt = realm.objects(DBDebt.self).filter(predicate).first else { fatalError("no debt found in DB for payment") }
let dbPayment = DBPayment(payment: payment)
try realm.write {
// first try
// dbDebt.paymentHistory.append(dbPayment)
// second try
let newPaymentHistory = dbDebt.paymentHistory
newPaymentHistory?.append(dbPayment)
dbDebt.paymentHistory = newPaymentHistory
}
}
注意,我包括在兩次嘗試我做了,無論這實際上持續數據的代碼(我檢查這個使用領域瀏覽器)。
另請注意,在調試時,我可以看到DBPayment
已創建並正確地追加到dbDebt
的列表中。也許我失去了一些東西很明顯,但我希望這在領域documentation因爲工作,他們聲明如下:
對於參考,這裏是定義我的結構和數據庫對象:
class DBDebt: Object {
dynamic var amount: Double = 0.0
dynamic var owner: DBPerson? = DBPerson()
var paymentHistory: List<DBPayment>? = List<DBPayment>()
dynamic var currency: String = Currency.Dollar.rawValue
dynamic var created: NSDate = NSDate()
dynamic var deadline: NSDate? = nil
init(debt: Debt) {
self.amount = debt.amount
self.owner = DBPerson(person: debt.owner)
self.paymentHistory = debt.paymentHistory.reduce(List<DBPayment>()) { (list: List<DBPayment>, payment: Payment) in
list.append(DBPayment(payment: payment))
return list
}
self.currency = debt.currency.rawValue
self.created = debt.created
self.deadline = debt.deadline
super.init()
}
required init(value: AnyObject, schema: RLMSchema) {
super.init(value: value, schema: schema)
}
required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init(realm: realm, schema: schema)
}
required init() {
super.init()
}
}
class DBDebt: Object {
dynamic var amount: Double = 0.0
dynamic var owner: DBPerson? = DBPerson()
var paymentHistory: List<DBPayment>? = List<DBPayment>()
dynamic var currency: String = Currency.Dollar.rawValue
dynamic var created: NSDate = NSDate()
dynamic var deadline: NSDate? = nil
init(debt: Debt) {
self.amount = debt.amount
self.owner = DBPerson(person: debt.owner)
self.paymentHistory = debt.paymentHistory.reduce(List<DBPayment>()) { (list: List<DBPayment>, payment: Payment) in
list.append(DBPayment(payment: payment))
return list
}
self.currency = debt.currency.rawValue
self.created = debt.created
self.deadline = debt.deadline
super.init()
}
required init(value: AnyObject, schema: RLMSchema) {
super.init(value: value, schema: schema)
}
required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init(realm: realm, schema: schema)
}
required init() {
super.init()
}
}
struct Payment {
init(amount: Double, paymentDate: NSDate = NSDate()) {
self.amount = amount
self.paymentDate = paymentDate
}
let amount: Double
let paymentDate: NSDate
}
class DBPayment: Object {
dynamic var amount: Double = 0.0
dynamic var paymentDate: NSDate = NSDate()
init(payment: Payment) {
self.amount = payment.amount
self.paymentDate = payment.paymentDate
super.init()
}
required init(value: AnyObject, schema: RLMSchema) {
super.init(value: value, schema: schema)
}
required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init(realm: realm, schema: schema)
}
required init() {
super.init()
}
}