首页 > 解决方案 > Realm in Swift relationship no add rows

问题描述

I am bulding app with relationship one to many. I can't add rows in my class.

class Bank : Object {

    @objc dynamic var id = 0
    @objc dynamic var allCash = 0
    let persons = List<Person>()

    override static func primaryKey() -> String? {
        return "id"
    }

}

class Person : Object {

    @objc dynamic var id = 0
    @objc dynamic var cash = 0

    let parent = LinkingObjects(fromType: Bank.self, property: "person")

    override static func primaryKey() -> String? {
        return "id"

    }

    func incrementID() -> Int {
        let realm = try! Realm()
        return (realm.objects(TransactionType.self).max(ofProperty: "id") as Int? ?? 0) + 1
    }

}

Save date in base

lat bank = Bank()
let persons = bank.persons
let parent = realm.objects(Bank.self).first!

try! realm.write {
    for person in persons {
        let newPerson = Person()
        newPerson.cash = person[cashTextField.text!] as! Int
        newPerson.id = person.incrementId()
        realm.add(newPerson)
        parent.persons.append(newPerson)   
    }    
}

When I pressed buton no errors and no added rows. I tried also this syntax and see row but it is with no relationship.

try! realm.write {
        let newPerson = Person()
        newPerson.cash = Int(cashTextField.text!)
        realm.add(newPerson)
}

标签: swiftrealm

解决方案


The code in your question really isn't too far off so we'll tighten it up a bit and get you going.

First, let's define customer and bank classes

class CustomerClass: Object {
    @objc dynamic var customer_id = NSUUID().uuidString
    @objc dynamic var cash = 0.0
    let parent = LinkingObjects(fromType: BankClass.self, property: "customerList")

    override static func primaryKey() -> String? {
        return "customer_id"
    }
}

class BankClass: Object {
    @objc dynamic var bank_id = NSUUID().uuidString
    @objc dynamic var cash_balance = 0.0
    let customerList = List<CustomerClass>()

    override static func primaryKey() -> String? {
        return "bank_id"
    }
}

as you can see there's a relationship that's defined as many to many; the bank can have multiple customers and a customer can have multiple banks. That works in this scenario and will allow you to add a customer to a bank and have the reverse relationship automatically created.

Then a function to add a customer to the first bank in the list. If there are multiple banks you would want to filter to get the bank you want to add the customer to but for this example, we'll just assume there's only one bank

func addCustomerToFirstBank() {
    if let realm = gGetRealm() {
        let bankResults = realm.objects(BankClass.self)
        if let firstBank = bankResults.first {
            let customerToAdd = CustomerClass()
            customerToAdd.cash = 50
            try! realm.write {
                firstBank.customerList.append(customerToAdd)
            }
         }
    }
}

A couple of things to note:

For brevity, we have a singleton that returns a realm when gGetRealm is called()

We are using a technique where swift will create a unique identifier for each bank and customer as their id. The key to the id is that it must be unique but it doesn't really matter what it is so if you are more comfortable using an incrementing index, that's fine as well.


推荐阅读