First off, I love Realm for SwiftUI and it has some amazing, efficient features, which only seem to be expanding as new versions are released. Amazing work!
Having said that, I am getting increasingly exasperated with thrown errors regarding realm instances. Most prominently, I am running into numerous frustrating instances of “Object is already managed by another Realm. Use create instead to copy it into this Realm.” I have also encountered an exception similar to “Objects must be from the realm being queried”.
These exceptions seem to imply that there are other realm databases in my project, which is not the case. I use a single, local realm. No other realm is every being queried, written to, or read from. My datamodel has many connected relations (I am importing the data from an existing SQL Server) and I usually ‘reach’ objects or lists of objects through following those relations and filtering them through a query where necessary. If I need to explicitly query the realm database, for example with a primary key, I exclusively use:
let realm = try! Realm()
Nevertheless, I seem to run into objects that are seemingly managed by other realms, as indicated by the exception(s) I mentioned above.
I suspect that this is, in fact, not another realm database, but either the same instance on a different thread, or another instance? (Not really sure what an instance means in this context) Or maybe a combination of the two, or maybe something else entirely? Either way, the exception is misleading, and on top of that there is very little (if any) documentation on this scenario.
So my feedback would be to either clarify these exceptions and/or add a section in the documents; unless, of course, I’m wrong and there are in fact multiple realm databases that I’ve created inadvertently, in which case the document could also do with a little explanation on how to avoid this. In other words: I am fully ready and willing to accept that these errors are the result of my own mistakes or misuse of the framework, but the documentation is unable to either help me avoid these mistakes, or rectify them.
For my specific use case, without going into too much detail, the exception is raised at the last line of the write block. The object is created literally right before that. The object has not been added, or created, to any instance of any realm anywhere, and the initializer only assigns the parameters to properties (for now). So how, where and why is this object possibly managed by another realm?
class ProjectServiceFloor: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var guid = UUID().uuidString
@Persisted var projectService: ProjectService!
@Persisted var floor: Floor!
...
func createInspectionPoint(of ipType: InspectionPointType) {
let realm = try! Realm()
let type = realm.object(ofType: InspectionPointType.self, forPrimaryKey: ipType.id)!
try! realm.write {
let newIp = InspectionPoint(type: type, floor: self.floor)
let newPip = ProjectInspectionPoint(ip: newIp, project: self.projectService.project)
let newPsip = ProjectServiceInspectionPoint(psf: self, pip: newPip)
realm.add(newPsip) // Error thrown here
}
}
Note that if the code doesn’t seem very optimal, it’s because this is just one of many iterations of debugging, trial-and-error and just generally metaphorically bashing my head in over this exception. (And also this data needs to be send back to an SQL Server that expects all these objects to be instantiated).
Also, I specifically query for the type because this function is called from a UI thread, which I believe works with a separate instance? Please correct me if this is not the case.