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

Object property is unreachable in the realm result #4

Closed
waltcow opened this issue Jan 21, 2019 · 4 comments
Closed

Object property is unreachable in the realm result #4

waltcow opened this issue Jan 21, 2019 · 4 comments

Comments

@waltcow
Copy link

waltcow commented Jan 21, 2019

Hi. I have some question about the realm result property, this is my first time to use realm

struct UserRealmProxy<RealmManager: UserRealmManager>: RealmProxiable {
   func getUser(_ userId: Int) -> User? {
        let result: RealmQuery<User> = query(filter: "id == \(userId)", sortProperty: "id", ordering: .ascending)
        return result.results.first
    }
}
    

When my service code want to fetch user from realm, the result is not nil, but the property from this realm object is still unreachable(empty) in the view render process. it's so weird.

@k-lpmg
Copy link
Owner

k-lpmg commented Jan 22, 2019

Hi @waltcow
I don't understand "realm object is still unreachable(empty) in the view render process".

Does the count of result continue to be 0?

@waltcow
Copy link
Author

waltcow commented Jan 23, 2019

   func getTweets(spaceId: Int, limit: Int = 15) -> [Tweet] {
        let result: RealmQuery<Tweet> = query(filter: "spaceId == \(spaceId)", sortProperty: "id", ordering: .ascending)
        var data = [Tweet]()
        for i in 0 ..< min(limit, result.count) {
            data.append(result[i])
        }
        
        return data
    }

fetch result from realm

image

cast model to viewModel in a dictionary key key model id

image

problem is the realm model property is still unreadable though i try to access it

@k-lpmg
Copy link
Owner

k-lpmg commented Jan 24, 2019

Is the "realm model property" that you want to approach is Realm class in RealmSwift?
If so, RealmProxiable can not be reached. Because it is generated in the query function and disappears.

UserRealmManager().transaction(writeHandler: { (realm) in
// Here you can access the realm.
})

or
You can create a realm using the createConfiguration function in RealmManageable.

let configuration = UserRealmManager().createConfiguration()
let realm = try! Realm(configuration: configuration)

Is that what I understand? If not, please explain what "realm model property" you want to approach.

@waltcow waltcow closed this as completed Jan 25, 2019
@waltcow
Copy link
Author

waltcow commented Jan 25, 2019

class Tweet: Object, Mappable {
   @objc var id: Int = -1
   @objc var spaceId: Int = -1
   @objc var content: String?
   @objc var userId: Int = -1
   ....
}

After i change my model definition to the follow, it works fine.

@objcMembers
class Tweet: Object, Mappable {
    dynamic var id: Int = -1
    dynamic var spaceId: Int = -1
    dynamic var content: String?
    dynamic var userId: Int = -1
   ....
}

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