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

Please, add description for work with realm #190

Open
NBibikov opened this issue Aug 28, 2017 · 1 comment
Open

Please, add description for work with realm #190

NBibikov opened this issue Aug 28, 2017 · 1 comment

Comments

@NBibikov
Copy link

Hello! thanks for great tool. Please, add description for mapping to realm Object

@toshi0383
Copy link

I think you just need to convert your Decodable model to a dictionary and pass it to Object.init(values:) initializer.

Recently I'm experimenting conversion using Sourcery. Though it hasn't supported full feature for Realm, it working ok for this simple model.

So for models like this,

protocol AutoRealmConvertible { }

class DownloadMediaRealm: Object {
    dynamic var id = ""
    dynamic var title = ""
    dynamic var url = ""
    dynamic var downloadedPath: String?
    override static func primaryKey() -> String? {
        return "id"
    }
}

struct DownloadMedia: AutoRealmConvertible {
    let id: String
    let title: String
    let url: String
    let downloadedPath: String?
}

with this Sourcery template,

// MARK: - AutoRealmConvertible for Enums (RealmOptional or List are not supported yet)
{% for type in types.implementing.AutoRealmConvertible %}
// MARK: - {{ type.name }} AutoRealmConvertible
extension {{ type.name }}: RealmConvertible {
    {{ type.accessLevel }} var allProperties: [String: Any?] {
        return [
            {% for variable in type.storedVariables where variable.readAccess != "private" and variable.readAccess != "fileprivate" %}"{{ variable.name }}": {{ variable.name }},
            {% endfor %}
        ]
    }
}
{% endfor %}

You would get auto generated source code like this.

// MARK: - AutoRealmConvertible for Enums (RealmOptional or List are not supported yet)
// MARK: - DownloadMedia AutoRealmConvertible
extension DownloadMedia: RealmConvertible {
    internal var allProperties: [String: Any?] {
        return [
            "id": id,
            "title": title,
            "url": url,
            "downloadedPath": downloadedPath,
        ]
    }
}

This is just my case. @ikesyo have any other options?

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

2 participants