Reputation: 4886
In Realm is there a way to set a unique id for every object stored in the database? I don't necessarily need it to be auto incremented just unique every time. I was just wondering is there a way to do it in Swift 2 on iOS 8 or above? I know you can do it in objC.
Upvotes: 3
Views: 2713
Reputation: 15103
To generate UUID for all objects in your Realm, you could create a superclass that generates the UUID:
class IdentifiableObject : Object {
private(set) dynamic var uuid = NSUUID().UUIDString
}
Swift5
class IdentifiableObject : Object {
private(set) dynamic var uuid = NSUUID().uuidString
}
While this works, it does come with a major catch, at least until realm supports read-only attributes. When updating objects, you will have to first obtain the stored object before saving.
Upvotes: 3