Reputation: 2652
Hello I have this object where I need to create unique identifiers ( primary keys ) for updating, retrieving etc from the realm database by identifier. Since realm doesn't support this out of the box I wanted to create my own way of identifying them.
class Swing: Object {
dynamic var latitude : Double = 0.0
dynamic var longitute : Double = 0.0
dynamic var speed : Double = 0.0
dynamic var date : NSDate = NSDate()
}
Can I use the NSDate as a unique id or should I go with a new column called id and use UUIDString? and is this always unique for a new object or only per device?
dynamic var id = NSUUID().UUIDString
What solution would be more failsafe/accurate?
Upvotes: 1
Views: 436
Reputation: 15991
It's probably safer to simply have an NSUUID
as your primary key. They're guaranteed to be unique, and depending on the types of NSDate
values you're saving to your property, uniqueness definitely can't be guaranteed.
NSUUID is a 128-bit randomly generated value, so even on different devices, the odds of the same string being generated twice is astronomically small.
class Swing: Object {
dynamic var id = NSUUID().UUIDString
dynamic var latitude : Double = 0.0
dynamic var longitute : Double = 0.0
dynamic var speed : Double = 0.0
dynamic var date : NSDate = NSDate()
override class func primaryKey() -> String? {
return "id"
}
}
Upvotes: 4