Hi there,
I’m writing a project using RealmSwift,
Now I kind of know some basic rules of using Realm, and here’s the quesiton:
how can I check if an Object is mamanged already? no seeing some similar APIs;
secondly,
imagine I have Object called “RealmOrder”, which contains an Object called “RealmItem”
now I have an initializer, which looks like this:
init(orderID: String, item: String) {
super.init()
orderId = orderID
rlmItem = RealmItem.getRealmItem(item)
}
RealmItem.getRealmItem(item) is defined as below:
static func getRealmItem(_ name: String) -> RealmItem {
let realm = try! Realm()
let item = realm.object(ofType: RealmItem.self, forPrimaryKey: name)
return item == nil ? RealmItem(name: name) : item
}
so basically if RealmItem already has data, it will return the managed object, if not it will create a new one.
Now problem is if I want to write this RealmOrder object into realm, I will get trouble like
‘Object is already managed by another Realm. Use create instead to copy it into this Realm.’
So I want to ask, how to solve this and what’s the best practise?