Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion Realm objects to dictionary: property mapping in nested arrays #314

Open
CallMeSnake opened this issue Jan 28, 2020 · 0 comments

Comments

@CallMeSnake
Copy link

There is a problem when you convert Realm objects using toDictionary(). When given realm object has nested array it uses toCodableValue() method from RealmListEVCustomReflectable.swift. There're conversion of every item in nested array to dictionary in this method:

    public func toCodableValue() -> Any {
        var q:[Any] = []
        for case let e as Any in self {
            q.append((e as? EVReflectable)?.toDictionary([.PropertyConverter, .KeyCleanup, .PropertyMapping, .DefaultSerialize]) ?? e)
        }
        return q
 
        // Why do we need all this code? Should be the same as this. But this crashes.
        //return self.enumerated().map { ($0.element as? EVReflectable)?.toDictionary() ?? NSDictionary() }
    }

The problem here is in conversion options: [.PropertyConverter, .KeyCleanup, .PropertyMapping, .DefaultSerialize]. This converts properties of items in nested array when you use .KeyCleanup

I've got object like this in my case :

    {
        id: 1,
        relativeId: 4,
        ...
        programRoles: [
            {
                id: 1,
                personId: 1,
                programId: 42,
                programType: 2
            }
        ]
    }

And after conversion I've got this dictionary with unexpected properties in array items:

    [
        {
            key: id,
            value: 1
        },
        {
            key: relativeId,
            value: 4
        },
        ...
        {
            key: programRoles,
            value: [
                [
                    {
                        key: id,
                        value: 1
                    },
                    {
                        key: person_id,
                        value: 1
                    },
                    {
                        key: program_id,
                        value: 42
                    },
                    {
                        key: program_type,
                        value: 2
                    }
                ]
            ]
        }
    ]

Can you look up this issue somehow? E.g. use conversionOptions passed from above in method toCodableValue() or just remove this hard-coded options?
Thnx for attention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant