-1

I have a lazy var in Swift like:

import Realm

class DataUser: RLMObject {

@objc dynamic lazy var id: String = self.myId()

@objc dynamic var firstTime: Int = 0

@objc dynamic var position: Int = 0

private func myId() -> String {
        return “\(firstTime)\(position)”
}

I’m getting this message:

** Terminating app due to uncaught exception 'RLMException', reason: 'Lazy managed property 'id' is not allowed on a Realm Swift object class. Either add the property to the ignored properties list or make it non-lazy.'

What is the correct way to use a lazy-variable in Swift and Realm?

Thanks!

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
user3745888
  • 6,143
  • 15
  • 48
  • 97
  • 2
    Possibly Duplicate of this https://stackoverflow.com/questions/38010548/compound-key-in-realm-with-lazy-property – Khushbu Desai Aug 24 '18 at 07:15
  • Possible duplicate of [Compound key in Realm with lazy property](https://stackoverflow.com/questions/38010548/compound-key-in-realm-with-lazy-property) – Dávid Pásztor Aug 27 '18 at 13:31
  • The suggested duplicate doesn't actually answer the question of how to ignore a managed property (or make it non managed), but rather deals with compound properties, which this doesn't seem to be. – David Berry Aug 27 '18 at 21:49
  • override getter for your id and make it lazy – Alexandr Kolesnik Jul 09 '19 at 07:34

2 Answers2

0

I believe you can use ignoreProperties() method for creating lazy var

Swift code:

public override static func ignoredProperties() -> [String] {
      return ["data"]
 }
Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
Qriti
  • 1
  • 1
  • 2
-1

You can directly use dynamic lazy var id: String = self.myId() also, and I don't think that your implementation is wrong. But Realm seems to not handling properly as your id is lazy. you can see bug report about the same also.

kchopda
  • 312
  • 4
  • 16