如何对Realm中的Linking Objects排序?查询崩溃问题咨询
The crash occurs because you're trying to sort the parent VolleyballPlayer entities using a field from their LinkingObjects<Player> collection, which isn't supported directly. Realm can't determine which specific Player's playerName to use for sorting a VolleyballPlayer (since each VolleyballPlayer can have multiple linked Player instances). Here's how to fix this based on your intended behavior:
1. Sort the Linked Players Collection for Each VolleyballPlayer
If your goal is to retrieve the linked Player instances in sorted order for each VolleyballPlayer, you can sort the RealmResults<Player> directly when accessing it from a VolleyballPlayer object:
// First get your VolleyballPlayers as usual RealmResults<VolleyballPlayer> volleyballPlayers = mRealm.where(VolleyballPlayer.class).findAll(); // For each VolleyballPlayer, get the sorted players for (VolleyballPlayer player : volleyballPlayers) { RealmResults<Player> sortedPlayers = player.getPlayers().sort("playerName"); // Use the sortedPlayers collection here }
This works because LinkingObjects returns a RealmResults instance, which you can sort using the sort() method just like any other Realm query result.
2. Sort VolleyballPlayers Based on Aggregated Player Data
If you need to sort the VolleyballPlayer entities themselves based on some property of their linked Players (e.g., the first player's name, or the lexicographically smallest name), you'll need to denormalize your data. Here's how:
Step 1: Add a Computed Field to VolleyballPlayer
Add a field to VolleyballPlayer that stores the value you want to sort by (e.g., firstPlayerName):
@RealmClass public class VolleyballPlayer extends RealmObject { @PrimaryKey private String id; @LinkingObjects("volleyballPlayer") private final RealmResults<Player> players = null; // New field to store the first player's name for sorting private String firstPlayerName; }
Step 2: Keep the Denormalized Field Updated
Use Realm's change listeners or write logic to update firstPlayerName whenever the linked Players change. For example, when adding a Player to a VolleyballPlayer:
// When adding a Player to a VolleyballPlayer mRealm.executeTransaction(realm -> { VolleyballPlayer volleyballPlayer = realm.where(VolleyballPlayer.class).equalTo("id", "some-id").findFirst(); Player newPlayer = realm.createObject(Player.class); newPlayer.setVolleyballPlayer(volleyballPlayer); newPlayer.setPlayerName("John Doe"); // Update the firstPlayerName if it's empty or you want to use the new player's name if (volleyballPlayer.getFirstPlayerName() == null || volleyballPlayer.getFirstPlayerName().isEmpty()) { volleyballPlayer.setFirstPlayerName(newPlayer.getPlayerName()); } });
Step 3: Sort VolleyballPlayers Using the Denormalized Field
Now you can safely sort the VolleyballPlayer entities using the denormalized field:
RealmResults<VolleyballPlayer> sortedVolleyballPlayers = mRealm.where(VolleyballPlayer.class) .sort("firstPlayerName") .findAll();
Why the Original Query Failed
The error Invalid query: field 'volleyballPlayer' in class 'VolleyballPlayer' is of invalid type 'LINKING_OBJECTS' happens because Realm doesn't allow sorting parent entities using fields from a LinkingObjects collection. LinkingObjects represents a one-to-many reverse relationship, so each parent has multiple children—there's no single value Realm can use to sort the parent.
内容的提问来源于stack exchange,提问作者propoLis

