如何按距离对RealmList或RealmResult进行排序? [英] How do you sort a RealmList or RealmResult by distance?

查看:122
本文介绍了如何按距离对RealmList或RealmResult进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个 RealmObject ,其中该字段latitudelongitude,并且您具有此类对象的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?.

想到的想法是:

  1. 添加一个字段calculatedDistance,遍历所有对象并进行设置.这种方法的问题在于,下次您调用
  2. 使用分组
  1. 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.
  2. 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".
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆