gpt4 book ai didi

android - 使用 Realm 和 LiveData。将 LiveData> 转换为 LiveData>

转载 作者:太空宇宙 更新时间:2023-11-03 12:44:48 28 4
gpt4 key购买 nike

我正在试用 Realm 以及包括 LiveData 在内的 Android 架构组件。

我一直在关注 Google 的应用程序架构指南:

https://developer.android.com/topic/libraries/architecture/guide.html

...用 Realm 代替 Room。

我有一切工作使用:

LiveData<RealmResults<CustomModelObject>>

从我的存储库层直接通过 ViewModel 到 View。

我认为从存储库返回更多通用类型可能会更好,所以 LiveData<List<CustomModelObject>>而不是 LiveData<RealmResults<CustomModelObject>> .

这是我遇到问题的代码 fragment :

@NonNull
@Override
protected LiveData<List<CustomModelObject>> loadFromDb() {
return Transformations.switchMap(customModelObjectsDao.getCustomModelObjects(),
new Function<RealmResults<CustomModelObject>, LiveData<List<CustomModelObject>>>() {
@Override
public LiveData<List<CustomModelObject>> apply(RealmResults<CustomModelObject> data) {
if (data == null) {
return AbsentLiveData.create();
} else {
return customModelObjectsDao.getCustomModelObjects();
}
}
});
}

customModelObjectsDao.getCustomModelObjects()当前返回 LiveData<RealmResults<Inspiration>> .

我想把它转换成LiveData<List<Inspiration>> .

我试过各种Transformations.mapTransformations.switchMap等都没有成功,我想我现在盯着它看得太久了:)

我是在正确的道路上还是遗漏了一些明显的东西?

非常感谢任何帮助。

谢谢,保罗。

更新

道:

public RealmLiveData<CustomModelObject> getCustomModelObjects() {
return asLiveData(realm.where(CustomModelObject.class).findAllAsync());
}

asLiveData 实现:

fun <T: RealmModel> RealmResults<T>.asLiveData() = RealmLiveData<T>(this)
fun Realm.CustomModelObjectsDao(): CustomModelObjectsDao = CustomModelObjectsDao(this)

更新 2

public class RealmLiveData<T> extends LiveData<RealmResults<T>> {

private RealmResults<T> results;

private final RealmChangeListener<RealmResults<T>> listener = new RealmChangeListener<RealmResults<T>>() {
@Override
public void onChange(RealmResults<T> results) {
setValue(results);
}
};

public RealmLiveData(RealmResults<T> realmResults) {
results = realmResults;
}

@Override
protected void onActive() {
results.addChangeListener(listener);
}

@Override
protected void onInactive() {
results.removeChangeListener(listener);
}
}

最佳答案

在您的情况下,替换 LiveData<RealmResults<T>LiveData<List<T>>足以解决您的问题。

但是,我建议尝试使用 RealmLiveResults the official example 中可用的类:

/**
* This class represents a RealmResults wrapped inside a LiveData.
*
* Realm will always keep the RealmResults up-to-date whenever a change occurs on any thread,
* and when that happens, the observer will be notified.
*
* The RealmResults will be observed until it is invalidated - meaning all local Realm instances on this thread are closed.
*
* @param <T> the type of the RealmModel
*/
public class LiveRealmResults<T extends RealmModel> extends LiveData<List<T>> {
private final RealmResults<T> results;

// The listener will notify the observers whenever a change occurs.
// The results are modified in change. This could be expanded to also return the change set in a pair.
private OrderedRealmCollectionChangeListener<RealmResults<T>> listener = new OrderedRealmCollectionChangeListener<RealmResults<T>>() {
@Override
public void onChange(@NonNull RealmResults<T> results, @Nullable OrderedCollectionChangeSet changeSet) {
LiveRealmResults.this.setValue(results);
}
};

@MainThread
public LiveRealmResults(@NonNull RealmResults<T> results) {
//noinspection ConstantConditions
if (results == null) {
throw new IllegalArgumentException("Results cannot be null!");
}
if (!results.isValid()) {
throw new IllegalArgumentException("The provided RealmResults is no longer valid, the Realm instance it belongs to is closed. It can no longer be observed for changes.");
}
this.results = results;
if (results.isLoaded()) {
// we should not notify observers when results aren't ready yet (async query).
// however, synchronous query should be set explicitly.
setValue(results);
}
}

// We should start observing and stop observing, depending on whether we have observers.

/**
* Starts observing the RealmResults, if it is still valid.
*/
@Override
protected void onActive() {
super.onActive();
if (results.isValid()) { // invalidated results can no longer be observed.
results.addChangeListener(listener);
}
}

/**
* Stops observing the RealmResults.
*/
@Override
protected void onInactive() {
super.onInactive();
if (results.isValid()) {
results.removeChangeListener(listener);
}
}
}

这样你的dao可以暴露LiveData<List<T>> ,还有你的 Transformations.map()应该可以。

关于android - 使用 Realm 和 LiveData。将 LiveData<RealmResults<CustomModelObject>> 转换为 LiveData<List<CustomModelObject>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49654211/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com