问题
I just started using Realm in my current android app and so far it is great. Unfortunately I´ve encountered a problem:
In my app the user can add different kind of entries (What did he eat this day? What drinks did he have?, etc.) into his diary. One DiaryEntry object represents the sum off all entries for a given day (e.g. 21.05.2017, etc.).
public class DiaryEntry extends RealmObject {
// ID of the day this diary entry represents
@PrimaryKey private Integer dateId;
private RealmList<MealEntry> mealEntries;
private RealmList<DrinkEntry> drinkEntries;
private RealmList<SymptomEntry> symptomEntries;
private RealmList<MedicineEntry> medicineEntries;
public void addMealEntry(MealEntry mealEntry) {
mealEntries.add(mealEntry);
}
public RealmList<MealEntry> getMealEntries() {
return mealEntries;
}
public void addDrinkEntry(DrinkEntry drinkEntry) {
drinkEntries.add(drinkEntry);
}
public RealmList<DrinkEntry> getDrinkEntries() {
return drinkEntries;
}
public void addSymptomEntry(SymptomEntry symptomEntry) {
symptomEntries.add(symptomEntry);
}
public RealmList<SymptomEntry> getSymptomEntries() {
return symptomEntries;
}
public void addMedicineEntry(MedicineEntry medicineEntry) {
medicineEntries.add(medicineEntry);
}
public RealmList<MedicineEntry> getMedicineEntries() {
return medicineEntries;
}
}
To display this data for a particular day in the diary, all entries should be sorted by the time the user created them. So every entry object contains a field 'time'.
private int time;
I came up with a temporary solution for my problem, but it´s far from perfect. The follwing code is executed on the UI Thread, which obviously is bad practice.
List<RealmObject> entryList = new ArrayList<>();
OrderedRealmCollectionSnapshot<MealEntry> mealEntries = diaryEntry.getMealEntries().createSnapshot();
OrderedRealmCollectionSnapshot<DrinkEntry> drinkEntries = diaryEntry.getDrinkEntries().createSnapshot();
OrderedRealmCollectionSnapshot<MedicineEntry> medicineEntries = diaryEntry.getMedicineEntries().createSnapshot();
OrderedRealmCollectionSnapshot<SymptomEntry> symptomEntries = diaryEntry.getSymptomEntries().createSnapshot();
entryList.addAll(mealEntries);
entryList.addAll(drinkEntries);
entryList.addAll(medicineEntries);
entryList.addAll(symptomEntries);
Collections.sort(entryList, entryComparator);
The code to sort the entry list uses reflection by invoking the getter method for the time field:
public int compare(RealmObject entry1, RealmObject entry2) {
try {
Method timeGetter1 = entry1.getClass().getMethod("getTime");
Method timeGetter2 = entry2.getClass().getMethod("getTime");
int time1 = (Integer) timeGetter1.invoke(entry1);
int time2 = (Integer) timeGetter2.invoke(entry2);
return time1 - time2;
} catch (NoSuchMethodException e) {
e.printStackTrace();
Timber.d("No such method 'getTime'.");
}
// Other catch clauses
As I said earlier, all of this happens on the UI thread.
I know that I can´t pass RealmObjects, RealmLists and RealmResults across threads so I really have a hard time coming up with an async solution for that. I thought of starting a background thread and in there create copies of all the RealmList´s inside a DiaryEntry object. Then merge this unmanaged lists and sort it - all of that on the background thread.
So my question: Are there any preferred strategies for merging multiple RealmLists and sorting the merged list - all of that in an async fashion? Would my attempt I described above work?
回答1:
Thanks @EpicPandaForce
I solved it exactly the way you described it and it works like a charm - now I even have the real-time functionality and no need to refresh the data manually, Nice :)
In case anybody faces the same problem I post some code pieces here that show how I solved it in code.
public class Entry extends RealmObject {
private static final int ENTRY_MEAL = 0;
private static final int ENTRY_DRINK = 1;
private static final int ENTRY_SYMPTOM = 2;
private static final int ENTRY_MEDICINE = 3;
/** The tag describes what kind of entry it represents */
private int tag;
/* Only one of these can be set, according to what this entry represents. */
@Nullable private MealEntry mealEntry;
@Nullable private DrinkEntry drinkEntry;
@Nullable private SymptomEntry symptomEntry;
@Nullable private MedicineEntry medicineEntry;
/** The time value this entry was created at */
/** Format: hours + minutes * 60 */
private int time;
public int getTime() {
return time;
}
/* Can only be accessed from within the 'data' package */
void setTime(int time) {
this.time = time;
}
/**
* Creates a new entry object in the realm database and tags it as 'MEAL'
*
* @param realm not null
* @param mealEntry the {@link MealEntry} object to map this entry to, not null
*
* @return the newly created entry
*/
static Entry createEntryAsMeal(@NonNull final Realm realm, @NonNull final MealEntry mealEntry) {
if(realm == null) {
throw new IllegalArgumentException("'realm' may not be null");
}
if(mealEntry == null) {
throw new IllegalArgumentException("'mealEntry' may not be null");
}
Entry entry = realm.createObject(Entry.class);
entry.tag = ENTRY_MEAL;
entry.mealEntry = mealEntry;
return entry;
}
/* Same methods for other tag types ... */
In MealEntry.class:
public class MealEntry extends RealmObject {
@PrimaryKey @Required private String id;
@Required private String title;
/** The entry objects this meal-entry is added to */
Entry entry;
/** This time value describes when the user consumed this meal **/
private int time;
// other fields
/**
* Creates a new MealEntry object in the realm.
* <p>
* Note: It is important to use this factory method for creating {@link MealEntry} objects in realm.
* Under the hood, a {@link Entry} object is created for every MealEntry and linked to it.
* </p>
*
* @param realm not null
*
* @return new MealEntry object which has been added to the <code>realm</code>
*/
public static MealEntry createInRealm(@NonNull Realm realm) {
if(realm == null) {
throw new IllegalArgumentException("'realm' may not be null");
}
MealEntry mealEntry = realm.createObject(MealEntry.class, UUID.randomUUID().toString());
mealEntry.entry = Entry.createEntryAsMeal(realm, mealEntry);
return mealEntry;
}
The 'time' field exists in the Entry.class and MealEntry.class so if the latter one changes the Entry must be updated accordingly:
/**
* Sets the time value for the <code>mealEntry</code> to the specified value.
* <p>
* Note: This method is necessary in order to sync the new time value with the underlying
* {@link Entry} object that is connected with the <code>mealEntry</code>.
* </p>
*
* @param mealEntry the {@link MealEntry} object to set the time for, not null
*
* @param time the new time value, must be in range of [0, 24*60] because of the format: hours*60 + minutes
*
*/
public static void setTimeForMealEntry(@NonNull MealEntry mealEntry, @IntRange(from=0, to=24*60) int time) {
if(mealEntry == null) {
throw new IllegalArgumentException("'mealEntry' may not be null");
}
mealEntry.setTime(time);
Entry entry = mealEntry.entry;
if(entry == null) {
throw new IllegalStateException("'mealEntry' contains no object of type 'Entry'! Something went wrong on creation of the 'mealEntry'");
}
/* Syncs the entries time value with the time value for this MealEntry. */
/* That´s important for sorting a list of all entries. */
entry.setTime(time);
}
Note: I could have stored only the ID of the corresponding Entry object inside the MealEntry and vice-versa for the Entry object store an ID to the corresponding MealEntry object. However I don´t know what a difference in perfomance this makes so I just went with the above approach. One reason for the other approach though would be that I wouldn´t have to store the 'time' field twice, once in the Entry.class and once in the MealEntry.class, because in the Entry.class I could just get the time value by finding the corresponding MealEntry object by its ID and then get the time.
来源:https://stackoverflow.com/questions/44094177/merge-multiple-realmlist%c2%b4s-and-sort-resulting-list