evermeer / EVReflection

Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift
Other
966 stars 119 forks source link

Realm List<String> fails to map in toDictionary(), so when passed to realm create method, crashes #277

Closed akramhussein closed 6 years ago

akramhussein commented 6 years ago

Using the following model, when I convert an object to an NSDictionary and insert using realm's create method, I receive a crash as seen below.

I've tried:

Model

import Foundation
import RealmSwift
import EVReflection

class Base: Object, EVReflectable {

    @objc dynamic var id: String = ""

    override static func primaryKey() -> String? { return "id" }
}

final class Person: Base {

    @objc dynamic var name: String = ""

    let aliases = List<String>()

    override static func primaryKey() -> String? { return "id" }

}

Error:

reason: 'Invalid value '{
}' of type '__NSDictionary0' for 'string' array property 'Person.aliases'.'
*** First throw call stack:

Seems its trying to parse an empty array, which is being stored as an empty NSDictionary.

akramhussein commented 6 years ago

I've checked and constructWith in extension List : EVCustomReflectable from file RealmListEVCustomReflectable.swift) is called.

evermeer commented 6 years ago

The error comes from the function RLMArrayValidateMatchingObjectType in RLMArray. As you can see in the error message the value is empty but it is a NSDictionary. I was able to reproduce the issue. If you have a List and to a .toDictionary() then EVReflection does not recognize it as a list of primitive types. Instead it tries to create a dictionary for every item. If you then try to create an object from that same dictionary, then you will get this error.

I will have a look if I can fix the .toDictionary() for List fields.

evermeer commented 6 years ago

Ah, there was a bug in the toCodableValue of RealmListEVReflectable.swift I will publish the fix now. It should be available in about an hour.

evermeer commented 6 years ago

It's published as version 5.5.6

akramhussein commented 6 years ago

Thank you so much - this fixed the issue 👍