Java 合并多个RealmList´;对结果列表进行排序?
我刚开始在我当前的android应用程序中使用Realm,到目前为止它非常棒。 不幸的是,我遇到了一个问题: 在我的应用程序中,用户可以将不同类型的条目(他今天吃了什么?喝了什么饮料?)添加到他的日记中。 一个日记条目对象表示给定日期(例如2017年5月21日等)所有条目的总和 我为我的问题想出了一个暂时的解决办法,但它还远远不够完美。 下面的代码是在UI线程上执行的,这显然是错误的做法Java 合并多个RealmList´;对结果列表进行排序?,java,android,realm,Java,Android,Realm,我刚开始在我当前的android应用程序中使用Realm,到目前为止它非常棒。 不幸的是,我遇到了一个问题: 在我的应用程序中,用户可以将不同类型的条目(他今天吃了什么?喝了什么饮料?)添加到他的日记中。 一个日记条目对象表示给定日期(例如2017年5月21日等)所有条目的总和 我为我的问题想出了一个暂时的解决办法,但它还远远不够完美。 下面的代码是在UI线程上执行的,这显然是错误的做法 List<RealmObject> entryList = new ArrayList<&
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);
如前所述,所有这些都发生在UI线程上
我知道我不能跨线程传递RealmObject
s、RealmList
s和RealmResult
s,所以我真的很难想出一个异步解决方案。我想启动一个后台线程,并在其中创建日志条目
对象中所有RealmList
的副本。然后合并这个非托管列表并对其进行排序——所有这些都在后台线程上进行
所以我的问题是:对于合并多个RealmList
s并对合并的列表进行排序,是否有任何首选策略?所有这些都是以异步方式进行的?我的上述尝试是否有效?谢谢@EpicPandaForce
我完全按照您描述的方式解决了它,它就像一个魔咒一样工作-现在我甚至有了实时功能,不需要手动刷新数据,很好:)
如果有人面临同样的问题,我会在这里发布一些代码片段,展示我是如何用代码解决的
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 ... */
在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;
}
Entry.class和MealEntry.class中存在“time”字段,因此如果后者发生更改,则必须相应更新条目:
/**
* 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);
}
注意:我本可以只在测量区内存储相应输入对象的ID,反之亦然。对于输入对象,将ID存储到相应的测量区对象。然而,我不知道这在性能上有什么不同,所以我只采用上述方法。
另一种方法的一个原因是,我不必存储“time”字段两次,一次存储在Entry.class中,一次存储在MealEntry.class中,因为在Entry.class中,我可以通过根据其ID查找相应的MealEntry对象来获取时间值,然后获取时间。您不能对RealmResults进行排序,在您的例子中,我认为您实际上想要的是“单表继承”,并且有一个带有
类型字段的条目类,该字段将是膳食,饮料,症状,药物。后台线程非托管+排序也可以工作,但您应该始终确保在这些RealmResults中的一个发生更改时(RealmChangeListener
)可以执行此过程——尽管这部分已经很棘手,因为对链接所做的更改不会在日记条目上调用RealmChangeListener。非常感谢,这个解决方案看起来绝对更干净,尽管它包含一些数据类的开销。实时数据更新弥补了这一缺陷;)
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 ... */
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;
}
/**
* 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);
}