swift - 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)
}
解决方案
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.
推荐阅读
- php - 当我在 CodeIgniter 中为数据表源选择多个数据时,我在 ORDER BY 中遇到问题,但出现 500 错误
- python - 在 Python 中使用正则表达式匹配小时:分钟时间格式
- api - 获取 GoToWebinar 注册人
- angular - Angular Material:我在单击按钮时以 Angular 形式再添加一行 matInput。但 matInput 提交的 UI 未正确加载
- c++ - 用于 Mac 的非 Apple C 编译器
- javascript - 从目录创建对象
- json - json.Unmarshal 忽略 json 字符串中的 json 字符串值,不要尝试解析它
- spring - 我想知道spring-data的命名约定
- html - 如何设置 html“选择”元素的选项位置?
- asynchronous - IRC | 异步通信