带OR运算符的领域主键 [英] Realm Primary key with OR operator

查看:101
本文介绍了带OR运算符的领域主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为新应用程序使用RealmSwift.我的Realm类有两个主键. 只是一个例子,我有一个像这样的领域模型(产品):-

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())
    }

因此,我正在设备中创建一个领域对象,并使用主键tempId将其存储到realm db中,因为实际的主键是id,这是服务器生成的主键,只有在报告同步后才会出现.因此,当我使用这些tempId服务器向服务器发送多个报告时,会以实际的id与每个tempId对应的方式响应我.由于该报告不仅是从我这边创建的,所以我不能保留tempId作为主键.我想到了Compound primary key,但是它不能解决问题.

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.

因此,我想创建一个主键,例如,如果存在id,则该主键为主键,否则tempId是主键.

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.

该怎么做?

推荐答案

本质上,您需要一个计算属性作为主键.但是,目前尚不支持此功能,只能将存储和托管的领域属性用作主键.一种解决方法是同时定义idtempId以具有显式的setter函数,并且在setter函数内部还需要设置另一个存储的属性,它将作为您的主键.

What you need essentially is a computed property as a primary key. However, this isn't supported at the moment, only stored and managed realm properties can be used as primary keys. A workaround could be to define both id and tempId to have explicit setter functions and inside the setter function you also need to set another stored property, which will be your primary key.

如果要更改idtempId,请不要通过通常的方式进行更改,而应通过其setter函数进行更改.

If you want to change id or tempId don't do it in the usual way, but do it through their setter function.

想法来自 GitHub问题.

Idea taken from this GitHub issue.

class Product: Object {
    dynamic var id:String? = nil
    dynamic var tempId: String? = nil

    func setId(id: String?) {
        self.id = id
        compoundKey = compoundKeyValue()
    }

    func setTempId(tempId: String?) {
        self.tempId = tempId
        compoundKey = compoundKeyValue() 
    }

    dynamic var compoundKey: String = ""
    override static func primaryKey() -> String? {
        return "compoundKey"
    }

    func compoundKeyValue() -> String {
        if let id = id {
            return id
        } else if let tempId = tempId {
            return tempId
        }
        return ""
    }
}

这篇关于带OR运算符的领域主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆