Compound key in Realm with lazy property
The solution you found is outdated. I'll leave a note about that there. I'd suggest removing the lazy
modifier and initializing compoundKey
to an empty string.
Realm Swift Composite Keys for Optional Properties
You need to set RealmOptional
's value
member. RealmOptional
properties cannot be var
because Realm can't detect assignment to property types that cannot be represented by the Objective-C runtime, which is why RealmOptional
, List
and LinkingObjects
properties must all be let
.
class Item: Object {
dynamic var id = 0
let importantNumber = RealmOptional<Int>()
let importantNumber2 = RealmOptional<Int>()
func setCompoundID(id: Int) {
self.id = id
compoundKey = compoundKeyValue()
}
func setCompoundImportantNumber(importantNumber: Int) {
self.importantNumber.value = importantNumber
compoundKey = compoundKeyValue()
}
func setCompoundImportantNumber2(importantNumber2: Int) {
self.importantNumber2.value = importantNumber2
compoundKey = compoundKeyValue()
}
dynamic lazy var compoundKey: String = self.compoundKeyValue()
override static func primaryKey() -> String? {
return "compoundKey"
}
func compoundKeyValue() -> String {
return "\(id)\(importantNumber)\(importantNumber2)"
}
}
Realm.io and compound primary keys
It appears that is the correct way to return a compound key in Realm.
Here's the answer from Realm : https://github.com/realm/realm-cocoa/issues/1192
You could use a mix of lazy and didSet instead to have the compoundKey
property be both derived and stored:public final class Card: Object {
public dynamic var id = 0 {
didSet {
compoundKey = compoundKeyValue()
}
}
public dynamic var type = "" {
didSet {
compoundKey = compoundKeyValue()
}
}
public dynamic lazy var compoundKey: String = self.compoundKeyValue()
public override static func primaryKey() -> String? {
return "compoundKey"
}
private func compoundKeyValue() -> String {
return "\(id)-\(type)"
}
}
// Example
let card = Card()
card.id = 42
card.type = "yolo"
card.compoundKey // => "42-yolo"
composite primary key realm/swift
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.
Related Topics
Xcode 7 Run on MAC Osx 10.10 Yosemite
Using @Discardableresult for Closures in Swift
Left Aligned Horizontal Stackview and Top Aligned Vertical Stackview
How to Open Your App's Settings (Inside the Settings App) with Swift (iOS 11)
Why Are Properties of an Immutable Object Mutable in Swift
How to Split a String into a [String] and Not [Substring]
Unwrapping Optional in Swiftui View
Swift Any Difference Between Closures and First-Class Functions
"Raw Value for Enum Case Is Not Unique" for Swift Enum with Float Raw Values
Zposition of Sknode Relative to Its Parent
How to Skip Iterations of a For-In Loop (Swift 3)
Compile Time Key Path Checking in Swift
Swift Generic Constraints in Init
Error "No Such Module" When Installed Framework with Pod in Swift 3