I am using RealmSwift for my new app. My Realm class has two primary keys. Just an example I have a Realm Model(Product) like this:-
class Product: Object, Mappable {
dynamic var id: String? = nil
dynamic var tempId: String? = nil
dynamic var name: String? = nil
dynamic var price: Float = 0.0
dynamic var purchaseDate: Date? = nil
required convenience init?(map: Map) {
self.init()
}
//I want to do something like this
override static func primaryKey() -> String? {
return "id" or "tempId"
}
func mapping(map: Map) {
id <- map["_id"]
tempId <- map["tempId"]
name <- map["name"]
price <- map["price"]
purchaseDate <- (map["purchaseDate"], DateFormatTransform())
}
So I am creating an realm object in my device and storing into realm db with primary key tempId
, as the actual primary key is the id
, which is a server generated primary key is coming only after the report sync. So when I am sending multiple reports to the server with those tempId
server response me back with actual id
mapped with each tempId
. As the report is not only created from my side so I can't keep tempId as the primary key. I thought of Compound primary key
but it won't solve the problem.
So I want to create a primary key such as If id
is there then that is the primary key else tempId
is the primary key.
How to do this?