0

I want to use Realm in mixed Objective-C & Swift app working with Codable and Realm Object can be export to Objective-C ;

class Person2 : RLMObject,Decodable {
      @objc dynamic var  name = ""
      convenience init(_  name:String) {
         self.init()
         self.name = name
      }
 }

class RepairShop2 : RLMObject,Decodable {
   @objc dynamic var name = ""
   @objc dynamic var contact:Person2?
   @objc dynamic var persons = RLMArray<Person2>(objectClassName: Person2.className())

   private enum RepairShop2CodingKeys: String, CodingKey {
       case name
       case contact
       case persons
   }

   convenience init(name: String, contact: Person2,  persons: RLMArray<Person2>) {
        self.init()
        self.name = name
        self.contact = contact
        self.persons = persons
   }

   convenience required init(from decoder: Decoder) throws {
      let container = try decoder.container(keyedBy: RepairShop2CodingKeys.self)
      let name = try container.decode(String.self, forKey: .name)
      let contact = try container.decode(Person2.self, forKey: .contact)
      let personArray = try container.decode(RLMArray<AnyObject>, forKey: .persons)   
 // this line error: Ambiguous reference to member 'decode(_:forKey:)'**

      let persons = RLMArray<Person2>(objectClassName: Person2.className())
      persons.addObjects(personArray)
      self.init(name: name, contact: contact, persons: persons)
  }
}

let personArray = try container.decode(RLMArray<AnyObject>, forKey: .persons)   
 // this line error: Ambiguous reference to member 'decode(_:forKey:)'**

RLMArray.self I also tried , fail

how to write decode type of RLMArray?

kingnight
  • 720
  • 6
  • 10

1 Answers1

0

RLMRealm doesn't conform to Decodable so you'll not be able to parse it into RLMRealm straight away. Instead try something like:

let persons = RLMArray<Person2>(objectClassName: Person2.className())
persons.addObjects(try container.decode([Person2].self, forKey: .persons) as NSFastEnumeration)

As a side note. Mixing different domains into one model is bad idea, it may bite later.

  • thanks, Evghenii Nicolaev, "Mixing different domains into one model is a bad idea" do you mean, class Person2 and RepairShop2 , both use as database and Model? I have thought of this question, but if I write a new model with same properties with Person2 and RepairShop2 , value assignment is repeated action, do you have any good suggestion? – kingnight Aug 01 '18 at 14:19
  • You can check next blog to have an idea of a possible implementation: https://medium.com/@gonzalezreal/using-realm-with-value-types-b69947741e8b As well, by doing so you'll lose some Realm features: https://stackoverflow.com/questions/34943550/realm-swift-models-separate-or-not Why I think this is "a bad idea" - you'll couple your implementation to Realm - realm objects are not thread safe - you'll have to deal with ugly workarounds(as one you have here) - realm objects carry a large amount of information, which is not needed(https://realm.io/docs/objc/3.7.2/api/Classes/RLMObject.html) – Evghenii Nicolaev Aug 01 '18 at 14:32