New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Property named id
not possible when using @Persisted property wrapper
#7372
Comments
This is just how the swift property wrapper feature works, so there probably isn't anything we can do about it. |
I have this same problem and I have a requirement to use a field named "id" in addition to "_id". Is there nothing that can be done with @persisted to work around this, or are we stuck doing our model classes the "old way"? And it's all or nothing, right? We can't mix @persisted with objc dynamic var because once we use @persisted once in a class, it will ignore the fields that use objc dynamic according to the docs:
|
A dumb workaround would be to define the Is your |
I could deal with the dumb workaround for now! I have ~20 model classes that all have an id property that I could use in a base class and extend that ~20 times into specific model classes with @persisted properties. That sounds pretty good to me for the time being. Yes, the _id property is just to conform to the Atlas/Realm Sync requirements that use that name automatically...although the part of our system that populates Atlas with data outside of Realm is using this _id name as well as it inserts documents into collection (or perhaps that's Atlas itself?) I'm trying this out and I'm getting many cases where I encounter this error:
When this happens, and I have a declaration in Swift that looks like
Does this mean that _id is nullable on the server side? Or, have I somehow become nullable on the client side? Also, if I declare a base class like
I get an error that I haven't defined an _id primary key for this class. If I try to put just the _id in the base class, when I extend the class, I get the same error as this original issue (synthesizing _id collides with the other definition of _id.) |
Ah, you'll need to put both That error message from sync should mean that the PK is optional on the server. It'd be nice if the error actually said which one was which, but I think the code doing the merging may not actually know that. |
Thanks @tgoyne ! Here is what worked for me today: (RealmSwift SDK 10.12)
I also needed to make both "_id" and "id" required properties on the Atlas Realm Schema end, so they would not be nullable. Data syncs, so we're good to go for now with this implementation. |
All my models have a primary key of The only thing i did today was integrate/update a few swiftui components we have, and that included adding https://github.com/realm/realm-cocoa/blob/v10.5.1/RealmSwift/Combine.swift#L38 Obviously this Still dont understand how schema is able able to get the string id in there, but that extension sure looks dangerous. |
https://github.com/realm/realm-cocoa/blob/master/Realm/RLMObject_Private.h#L83 This is confusing, i don't understand the motivation. Is this MongoDB specifics leaking into the SDK or something? My models already conform to
This is confusing. Are you just trying to say if your model doesn't have a primary key, you can't conform to |
ObservedRealmObject shouldn't require ObjectKeyIdentifiable and I think that's just an oversight. ObjectKeyIdentifiable is intended as just a convenience helper for making types without primary keys conform to Identifiable. |
#7391 relaxes the protocol requirement from ObjectKeyIdentifiable to Identifiable. |
When declaring a class with
@Persisted
properties, a property named_id
cannot exist alongside a property namedid
.Code Sample
This scenario works when using the old
@objc dynamic var
declaration of properties:Version of Realm and Tooling
Realm framework version: 10.11.0
Xcode version: 12.5.1
iOS/OSX version: 14.5
Dependency manager + version: Carthage 0.38.0
The text was updated successfully, but these errors were encountered: