composite primary key realm/swift

后端 未结 3 1695
攒了一身酷
攒了一身酷 2020-12-14 06:40

I\'m new to swift and realm. I want to make a composite primary key and when I\'m trying something like this :

class DbLocation : Object {
 dynamic var id =         


        
相关标签:
3条回答
  • 2020-12-14 07:20

    Simple create a new property whose value is set to interested other properties which you expect to be compound primary keys.

    class DbLocation: Object {
                dynamic var id = 0
                dynamic var tourId = 0
                dynamic var compoundKey: String? = ""
    
            override static func primaryKey() -> String? {
                    return "compoundKey"
                }
            }
        let location = DbLocation()
        location.tourId = 1
        location.id = 5
        location.compoundKey = "\(id) \(tourId)"
    
    0 讨论(0)
  • 2020-12-14 07:40

    For latest version of Swift and Realm, I would do something like this.

    dynamic private var compoundKey: String = ""
    
    required convenience init?(map: Map) {
      self.init()
      if let firstValue = map.JSON["firstValue"] as? String,
        let secondValue = map.JSON["secondValue"] as? Int {
        compoundKey = firstValue + "|someStringToDistinguish|" + "\(secondValue)"
      }
    }
    
    0 讨论(0)
  • 2020-12-14 07:42

    For 1.0.1+ of Realm:

    class DbLocation: Object{
        dynamic var id = 0
        dynamic var tourId = 0
        dynamic var compoundKey = ""
    
        override static func primaryKey() -> String? {
            return "compoundKey"
        }
    
        func setup(id: Int, tourId: Int){
            self.id = id
            self.tourId = tourId
            self.compoundKey = compoundKeyValue()
        }
    
        func compoundKeyValue() -> String {
            return "\(id)\(tourId)"
        }
    }
    

    Usage example:

    let location = DbLocation()
    location.setup(id: 0, tourId: 1) 
    print(location.compoundKey) // "01"
    

    Of course you can play around with using various didSet listeners on id and tourId, to make sure that compoundKey gets properly rewritten every time the values get changed.

    For pre-1.0.1 of Realm:

    class DbLocation: Object {
        dynamic var id = 0
        dynamic var tourId = 0
    
        func setCompoundID(id: Int) {
            self.id = id
            compoundKey = compoundKeyValue()
        }
    
        func setCompoundTourId(tourId: Int) {
            self.tourId = tourId
            compoundKey = compoundKeyValue()
        }
    
        dynamic lazy var compoundKey: String = self.compoundKeyValue()
    
        override static func primaryKey() -> String? {
            return "compoundKey"
        }
    
        func compoundKeyValue() -> String {
            return "\(id)\(tourId)"
        }
    }
    

    The custom setters make sure, that the compoundKey is always updated, the lazy key word makes sure that the first time you access it, it will be derived from what you've already set.

    Find out more on this topic in this thread where this issue has been debated.

    0 讨论(0)
提交回复
热议问题