Zhuinden / realm-monarchy

[ACTIVE-ISH] A wrapper over Realm which exposes it as LiveData, managing Realm lifecycle internally.
Apache License 2.0
88 stars 11 forks source link

greaterThanOrEqualTo not working as expected #19

Closed squirtle1990 closed 6 years ago

squirtle1990 commented 6 years ago

I am attempting to query all ClassName objects with a particular symbol, type, and time. Because Realm famously rounds off date milliseconds, I opted to store dates as doubles with the time in milliseconds. In the query below, I am attempting to get all objects from the past 24 hours, so I query for objects with time greaterThanOrEqualTo the millisecond time from a date 24 hours in the past. Only problem is, I receive zero objects in the result query, unless I lower the time to some ridiculously low value, like 0.

 quotes = monarchy.findAllMappedWithChanges({ realm -> realm.where(ClassName::class.java).equalTo("symbol", symbol).greaterThanOrEqualTo("time", oneDayAgo).equalTo("type", "minute").sort("time", Sort.ASCENDING) }, { input ->
                        Quote.create(input.symbol, input.price, input.volume, input.time, input.type)
                    })

I have verified client side through LogCat the time object is in milliseconds, and server side through Realm Studio that the time fields are in milliseconds as well.

It seems strange to say the least.

Zhuinden commented 6 years ago

Because Realm famously rounds off date milliseconds

Realm stores miliseconds properly since... well a very long time ago actually, afaik.

But maybe this is something I'm just not aware of?

However, I don't think this is caused by anything I'm doing. It could be a timezone question, Realm Studio afaik shows dates in GMT.

squirtle1990 commented 6 years ago

I checked the raw values in Logcat and Realm Studio. The GMT time zones are both set to the same on both platforms.

Zhuinden commented 6 years ago

Does it work properly if you use Realm directly instead of Monarchy?

squirtle1990 commented 6 years ago

Hmm no, it doesn't seem to work with Realm directly either. It might be an issue with the large double values - perhaps a catastrophic overflow. I'm going to replace the time representation with a Date object instead and try again.

Zhuinden commented 6 years ago

You might be hit by https://github.com/realm/realm-core/issues/3076

squirtle1990 commented 6 years ago

Switching to Date fields in Realm solved the problem.