我有一个传递paymentdebt对象的函数(两者都是表示领域数据库中数据的结构)。然后我想将payment附加到paymentHistorydebt中。下面是我使用的代码:

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

请注意,我包含了两次尝试的代码,这两次尝试都没有保存数据(我使用Realm浏览器检查了这一点)。
另外请注意,在调试时,我可以看到DBPayment已经创建并正确地附加到dbDebt的列表中。也许我遗漏了一些显而易见的东西,但我希望这能起作用,因为在documentation领域中,它们声明如下:
ios -  Realm 不保留写入中所做的更改-LMLPHP
以下是我的structs和DB对象的定义,供参考:
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()
  }

}

最佳答案

为了在领域中持久化对象,必须在领域(docs)上调用add()函数。
以下方法应该有效:

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
}

10-07 14:17