如何按距离对RealmList或RealmResult进行排序? [英] How do you sort a RealmList or RealmResult by distance?
问题描述
给出一个 RealmObject ,其中该字段latitude
和longitude
,并且您具有此类对象的RealmList或RealmResult.您如何根据距离排序,例如当前的位置?.
Given a RealmObject that has the fields latitude
and longitude
and you have a RealmList or a RealmResult of such objects. How do you sort by distance by for example your current Location?.
想到的想法是:
- Add a field
calculatedDistance
, iterate over all objects and set it. The problem with this approach is that the next time you call copyToRealmOrUpdate(..) you'll overwrite this field with null. - Call Collections.sort(...) on the list or result with my own custom Comparator but I can't find it documented anywhere the behavior of a managed RealmList or RealmResult when they're sorted by "outsiders".
- Do a really advanced query using groupings and between(..) etc - but I can't come up with a good idea.
Android上的任何人都找到了一个好的解决方案?
Anyone on Android found a good solution?
推荐答案
像这样的事情确实需要地理空间支持才能以任何有效的方式工作.
Something like this really requires Geo spatial support to work in any efficient manner.
现在,我认为您最好的选择是从Realm中复制数据,并在辅助线程中进行手动处理.
Right now I think your best bet is copying the data out of Realm and do manual processing in a worker thread.
如果在HandlerThread中实现它,则可以执行类似的操作
If you implement it in a HandlerThread you can do something like this
RealmResults<City> results = realm.where(City.class).findAllAsync();
results.addChangeListener(new RealmChangeListener<Results<City>>() {
@Override
public void onChange(RealmResults<City> cities) {
List<City> standaloneCities = realm.copyFromRealm(cities);
doManualSorting(standaloneCities);
sendToUiThread(standaloneCities);
}
});
这篇关于如何按距离对RealmList或RealmResult进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!