如何排序领域的链接对象?

时间:2018-01-15 08:42:32

标签: android sorting realm rlmlinkingobjects

我有一个包含linkedObject的realmObject。

@RealmClass
public class VolleyballPlayer extends RealmObject {

    @PrimaryKey
    private String id;

    @LinkingObjects("volleyballPlayer")
    private final RealmResults<Player> players = null;
}

@RealmClass
public class Player extends RealmObject {

    private Set set;
    private VolleyballPlayer volleyballPlayer;
    private BasketballPlayer basketballPlayer;
    private String playerName = "";
}

我想对realmObject内部的对象进行排序。但是第一个查询正在运行,但由于已排序的链接对象,其他查询正在崩溃。

     RealmResults<VolleyballPlayer> volleyballPlayers = 
   mRealm.where(VolleyballPlayer.class)
.findAll();

     RealmResults<VolleyballPlayer> volleyballPlayers = 
  mRealm.where(VolleyballPlayer.class)
.sort("link.playerName") 
.findAll();

我的崩溃报告:

  java.lang.IllegalArgumentException: 
Invalid query: field 'volleyballPlayer' in class 'VolleyballPlayer' is of invalid type 'LINKING_OBJECTS'.
                                                                              at io.realm.internal.fields.FieldDescriptor.verifyColumnType(FieldDescriptor.java:277)
                                                                              at io.realm.internal.fields.FieldDescriptor.verifyInternalColumnType(FieldDescriptor.java:230)
                                                                              at io.realm.internal.fields.CachedFieldDescriptor.compileFieldDescription(CachedFieldDescriptor.java:83)
                                                                              at io.realm.internal.fields.FieldDescriptor.compileIfNecessary(FieldDescriptor.java:285)
                                                                              at io.realm.internal.fields.FieldDescriptor.getFinalColumnType(FieldDescriptor.java:213)
                                                                              at io.realm.internal.SortDescriptor.checkFieldType(SortDescriptor.java:110)
                                                                              at io.realm.internal.SortDescriptor.getInstance(SortDescriptor.java:95)
                                                                              at io.realm.internal.SortDescriptor.getInstanceForSort(SortDescriptor.java:65)
                                                                              at io.realm.RealmQuery.sort(RealmQuery.java:1953)
                                                                              at io.realm.RealmQuery.sort(RealmQuery.java:1914)
                                                                              at io.realm.RealmQuery.sort(RealmQuery.java:1898)
                                                                              at com.android.android.dao.crud.ShareCRUD$1.run(ShareCRUD.java:40)
                                                                              at com.android.android.dao.RealmController$1.run(RealmController.java:60)
                                                                              at java.lang.Thread.run(Thread.java:762)

1 个答案:

答案 0 :(得分:2)

Realm不支持在链接对象中进行排序。因此,我将name字段添加到VolleyballPlayer

@RealmClass
public class VolleyballPlayer extends RealmObject {

    @PrimaryKey
    private String id;
    private String name = "";

    @LinkingObjects("volleyballPlayer")
    private final RealmResults<Player> players = null;
}

排序是:

RealmResults<VolleyballPlayer> volleyballPlayers = 
  mRealm.where(VolleyballPlayer.class)
  .sort("name") 
  .findAll();

谢谢