最近,我坚持使用以下代码.
public class Noteviewmodel extends viewmodel { private final mutablelivedata<List<Note>> notesliveData = new mutablelivedata<>(); public Noteviewmodel() { liveData<List<Note>> notesliveDataFromrepository = NoteRepository.INSTANCE.getNotes(); // How can I "assign" liveData from Room, to mutablelivedata? }}
我想知道,如何将Room中的liveData“分配”到mutablelivedata?
使用transformation.map和transformation.switchMap不起作用,因为它们都返回liveData,而不是mutablelivedata.
可能的解决方法
其中一个可能的解决方案是,而不是
@Daopublic abstract class NoteDao { @Transaction @query("SELECT * FROM plain_note") public abstract liveData<List<Note>> getNotes();
我会用的
@Daopublic abstract class NoteDao { @Transaction @query("SELECT * FROM plain_note") public abstract List<Note> getNotes();
然后,在我的viewmodel中,我会写
public class Noteviewmodel extends viewmodel { private final mutablelivedata<List<Note>> notesliveData = new mutablelivedata<>(); public Noteviewmodel() { new Thread(() -> { List<Note> notesliveDataFromrepository = NoteRepository.INSTANCE.getNotes(); notesliveData.postValue(notesliveDataFromrepository); }).start(); }}
我真的不喜欢这种方法,因为我被迫明确处理线程化问题.
有没有更好的方法,以避免明确处理线程?
解决方法:
诀窍是不在视图模型中进行任何实际的提取.
从网络或数据库获取数据应该在存储库中完成.在这方面,viewmodel应该是不可知的.
在viewmodel中,使用liveData类,而不是mutablelivedata.除非你真的找到它的用例.
// In your constructor, no extra threadnotesliveData = notesliveDataFromrepository.getAllNotes();
然后在您的存储库中,您可以使用getAllNotes()方法中的逻辑来确定这些注释的来源.在存储库中,您拥有mutablelivedata.然后,您可以从获取数据的线程postValue.这对于房间来说不是必需的,这是为你处理的.
因此,在您的存储库中,您将返回另一个直接从DAO方法支持的liveData.
在这种情况下,您需要坚持使用公共抽象liveData< List< Note>> getNotes();.
活动
public class MyActivity extends AppCompatActivity { private Myviewmodel viewmodel; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set up your vIEw model viewmodel = viewmodelProvIDers.of(this).get(Myviewmodel.class); // Observe the vIEw model viewmodel.getMyliveData().observe(this, s -> { // You work with the data provIDed through the vIEw model here. // You should only really be delivering UI updates at this point. Updating // a RecyclerVIEw for example. Log.v("liVEDATA", "The livedata changed: "+s); }); // This will start the off-the-UI-thread work that we want to perform. MyRepository.getInstance().doSomeStuff(); }}
视图模型
public class Myviewmodel extends AndroIDviewmodel { @NonNull private MyRepository repo = MyRepository.getInstance(); @NonNull private liveData<String> myliveData; public Myviewmodel(@NonNull Application application) { super(application); // The local live data needs to reference the repository live data myliveData = repo.getMyliveData(); } @NonNull public liveData<String> getMyliveData() { return myliveData; }}
知识库
public class MyRepository { private static MyRepository instance; // Note the use of mutablelivedata, this allows changes to be made @NonNull private mutablelivedata<String> myliveData = new mutablelivedata<>(); public static MyRepository getInstance() { if(instance == null) { synchronized (MyRepository.class) { if(instance == null) { instance = new MyRepository(); } } } return instance; } // The getter upcasts to liveData, this ensures that only the repository can cause a change @NonNull public liveData<String> getMyliveData() { return myliveData; } // This method runs some work for 3 seconds. It then posts a status update to the live data. // This would effectively be the "doInBackground" method from AsyncTask. public voID doSomeStuff() { new Thread(() -> { try { Thread.sleep(3000); } catch (InterruptedException ignored) { } myliveData.postValue("Updated time: "+System.currentTimeMillis()); }).start(); }}
总结 以上是内存溢出为你收集整理的android – 如何在ViewModel中将LiveData从Room“分配”到MutableLiveData全部内容,希望文章能够帮你解决android – 如何在ViewModel中将LiveData从Room“分配”到MutableLiveData所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)