2016-08-05 43 views
0

我有一個函數,我通過一個payment和一個debt對象(都是代表我的Realm數據庫中的數據的結構)。然後,我想將payment附加到debtpaymentHistory。下面是我用這個代碼:境界不堅持寫入的變化

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因爲工作,他們聲明如下:

enter image description here

對於參考,這裏是定義我的結構和數據庫對象:

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() 
    } 

} 

回答

2

爲了堅持對象的領域,你必須調用你的領域add()函數(docs)。

下面應該工作:

try realm.write { 

    // this persists your dbPayment in your realm! 
    realm.add(dbPayment) 

    // first try 
    // dbDebt.paymentHistory.append(dbPayment) 

    // second try 
    let newPaymentHistory = dbDebt.paymentHistory 
    newPaymentHistory?.append(dbPayment) 
    dbDebt.paymentHistory = newPaymentHistory 
}