I have a bunch of methods saving camera names related to a video recording asynchronously in Realm database.
My model:
/**
Realm object for saving the camera names used
to record clip videos
*/
public class RMCamera: Object {
dynamic var id = ""
dynamic var name = ""
override public class func primaryKey() -> String {
return "id"
}
}
The method saving the camera's in Realm:
/**
Saves camera names for offline usage after deleting
all current entries first.
- parameter cameraNames: An array of string with the camera names to save
*/
public func saveCameraNames(cameraNames: [String]) {
logv(" Saving camera names...")
concurrentQueueBg.async {
let realm = try! Realm()
self.deleteCamerasFromRealm(realm)
cameraNames.forEach { camera in
let offlineCamera = RMCamera(value: ["id": NSUUID().UUIDString, "name": camera])
do {
try realm.write {
realm.add(offlineCamera)
}
} catch {
loge("Error adding offline camera")
}
}
}.notify(.Main) {
logv("✅ Camera names saved!")
}
}
And the method that first purges all camera entries from Realm:
/**
Deletes all camera names saved in the passed Realm
- parameter realm: A Realm database instance to use
*/
func deleteCamerasFromRealm(realm: Realm) {
let cameras = realm.objects(RMCamera)
try! realm.write {
realm.delete(cameras)
}
}
The problem is that every once in a while I am getting errors like this one:
Can't set primary key property 'id' to existing value '572cb087b974c25b01fa40c1'.
Any ideas what might be actually going wrong? I really don't think I am getting collisions from UUID generation.