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

Wrong extraction of some properties #37

Open
d-yurchenko opened this issue Jul 8, 2020 · 5 comments
Open

Wrong extraction of some properties #37

d-yurchenko opened this issue Jul 8, 2020 · 5 comments

Comments

@d-yurchenko
Copy link

d-yurchenko commented Jul 8, 2020

Was testing simple struct:

struct Foo: Realmable {
    
    let id: Int
    let name: String
    let readOnly: Bool
    let enabled: Bool
    
    static func primaryKey() -> String? {
        return "id"
    }
    
    init(id: Int, name: String, readOnly: Bool, enabled: Bool) {
        self.id = id
        self.name = name
        self.readOnly = readOnly
        self.enabled = enabled
    }
    
    init() {
        self.init(id: -1, name: "", readOnly: true, enabled: false)
    }
}

like this:

guard let realm = try? Realm() else { return }
let foo = Foo(id: 0, name: "test name", readOnly: false, enabled: true)
try? realm.write {
    realm.add(foo)
}
let foos = realm.objects(Foo.self).map({ $0 })

Found out problem with extracting properties with 0 or false value. Problem appears at this func:

Screenshot 2020-07-08 at 17 40 41

guard let value = obj.value(forKey: propertyName) fails for such properties and they left default:

Screenshot 2020-07-08 at 17 37 53

@arturdev
Copy link
Collaborator

arturdev commented Jul 8, 2020

Instead of this
self.init(id: -1, name: "", readOnly: true, enabled: false)
do this:

struct Foo: Realmable {
    
    let id: Int = -1
    let name: String = ""
    let readOnly: Bool = false
    let enabled: Bool = false
    
    static func primaryKey() -> String? {
        return "id"
    }
    
    init(id: Int, name: String, readOnly: Bool, enabled: Bool) {
        self.id = id
        self.name = name
        self.readOnly = readOnly
        self.enabled = enabled
    }
    
    init() {
    }
}

@d-yurchenko
Copy link
Author

You can do this only with var properties, otherwise you'll have an error "Immutable value 'self.id' may only be initialized once".

Even then the problem won't be solved, mutating func readValues(from obj: Object) is from Unrealm.swift. In Objc 0 == nil, and obj.value(forKey: propertyName) returns nil instead of 0. I guess this func doesn't have to work like this.

You can define default value as 0, but this is crutch.

@d-yurchenko
Copy link
Author

d-yurchenko commented Jul 8, 2020

struct Foo: Realmable {
    
    var id: Int = -1
    var name: String = ""
    var readOnly: Bool = true
    var enabled: Bool = false
    
    static func primaryKey() -> String? {
        return "id"
    }
    
    init(id: Int, name: String, readOnly: Bool, enabled: Bool) {
        self.id = id
        self.name = name
        self.readOnly = readOnly
        self.enabled = enabled
    }
    
    init() {}
}

This is the result for your variant. Unrealm still couldn't read 0 from obj: Object

Screenshot 2020-07-08 at 21 39 17

@arturdev
Copy link
Collaborator

arturdev commented Jul 9, 2020

Could you please provide a minimal reproducible example project so that I can run it locally and test/fix?

@d-yurchenko
Copy link
Author

d-yurchenko commented Jul 9, 2020

I've managed to fix problem with Int and Bool values (actually don't sure how good is my solution), but there's still problem with Float.

In Podfile set path to my forked repo, remove it to test it with origin.

Unrealm_test.zip

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