Search code examples
swiftrealmrealm-mobile-platform

SwiftRealm how to check if a Realm Object has a primary key or not?


Sometimes we may need to check if the realm object has primary key or not. Like if we want to do func add<S>(_ objects: S, update: UpdatePolicy = .error) where S : Sequence, S.Element : Object. If an object already exists in the Realm with the specified primary key and the update policy is .modified or .all. The .error is for objects without a primary key.


Solution

  • using object.objectSchema.primaryKeyProperty == nil to check if the object has the primary key or not.