在使用Cloud Firestore后端实施ViewModel类时需要帮助 [英] Need help in implementing ViewModel class with Cloud Firestore backend

查看:45
本文介绍了在使用Cloud Firestore后端实施ViewModel类时需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序从Cloud Firestore提取一些文档,并在RecyclerView中显示文档的内容.我想遵循关注点分离的原则,因此使用MVVM体系结构来构建应用程序.我已经使用FirestoreRecyclerAdapter为RecyclerView实现了数据类模型和适配器.

I am developing an application that fetches some documents from the Cloud Firestore and shows the contents of the documents in a RecyclerView. I want to follow the principle of separation of concern, and therefore using the MVVM architecture to build the app. I have implemented a Data class model and the adapter for the RecyclerView using the FirestoreRecyclerAdapter.

我已经从事了一个指导项目,该项目将MVVM架构与LiveData和Room一起使用.在该项目中,实现的适配器具有一个ArrayList,该ArrayList包含所有数据,这些数据又用于填充RecyclerView.

I have already worked on a guided project which used MVVM architecture with LiveData and Room. In that project, the adapter implemented had an ArrayList which held all the data which was in turn used to populate the RecyclerView.

viewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(application)).get(NoteViewModel::class.java)
viewModel.allNotes.observe(this, { list->list?.let{
            adapter.updateList(it)}
        })

类似地,我从事的项目使用Firestore,但未使用MVVM体系结构.在后一个版本中,RecyclerView充斥了数据,而没有将任何数据传递给任何集合.它甚至没有使用LiveData,但是Firestore中的更改会立即反映出来(我相信这是Cloud Firestore的许多优点之一).

Similarly, I have worked on a project which used Firestore but did not employ the MVVM architecture. In the latter one, the RecyclerView got inflated with data without passing any data to any collection whatsoever. It did not even use LiveData but the changes in Firestore were reflected instantly (which I believe is one of many benefits of Cloud Firestore).

postDao = PostDao()
val postcollection = postDao.postCollections
val query = postcollection.orderBy("createdAt", Query.Direction.DESCENDING)
val recyclerviewOptions = FirestoreRecyclerOptions.Builder<Post>().setQuery(query, Post::class.java).build()


adapter = PostAdapter(recyclerviewOptions)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)

如上面的代码所示,只需将集合传递给适配器,就可以使RecyclerView膨胀.

As in the code above, simply passing the collection to the adapter inflated the RecyclerView.

但是现在在这个我要实现体系结构的项目中,ViewModel类遇到了问题.它应该包含什么?以及如何将FirestoreRecyclerOptions与ViewModel一起使用?我是否需要一个集合(例如List或ArrayList)来将DAO中的数据包装到LiveData中?还是我完全需要LiveData?参考文档指出,我应该在某些对象或集合上使用MutableLiveData,然后我应该有一种方法可以在每次数据更改时更新列表.还实现了一个观察者方法.但是我看不到有关Firestore的任何解释,甚至看不到FirestoreRecyclerAdapter的工作方式.我需要在ViewModel类中实现关于数据库集合上MutableLiveData的实现,并在Cloud Firestore的上下文中更新FirestoreRecyclerAdapter中的功能.

But now in this project where I want to implement the architecture, I am having an issue with the ViewModel class. What should it contain? And how do I use the FirestoreRecyclerOptions with the ViewModel? Do I need a collection (like a List or an ArrayList) to wrap the data from the DAO in the LiveData? Or do I need the LiveData at all? The reference docs state that I should use MutableLiveData on some object or collection, and then I should have a method to update the list every time the data changes. There is also an observer method that is implemented. But I don't see any explanation about Firestore or how the FirestoreRecyclerAdapter is even working. What I need is what do I need to implement in the ViewModel class in respect to MutableLiveData on collections from the database and update functions in the FirestoreRecyclerAdapter in the context of Cloud Firestore.

推荐答案

类似地,我从事的项目使用Firestore,但未使用MVVM体系结构.在后一个版本中,RecyclerView充斥了数据,而没有将数据传递给任何集合.

Similarly, I have worked on a project which used Firestore but did not employ the MVVM architecture. In the latter one, the RecyclerView got inflated with data without passing the data to any collection whatsoever.

之所以发生这种情况,是因为您使用的是适用于Android的Firebase-UI库,这意味着这个图书馆在后台做了繁重的工作.您要做的是仅创建一个"FirestoreRecyclerOptions"对象,将其传递给适配器,就差不多了.

That's was happening because you were using Firebase-UI library for Android, meaning that all the heavy work was done behind the scenes by this library. What you had to do, is to create only a "FirestoreRecyclerOptions" object, pass it to the adapter, and that's pretty much all of it.

它甚至没有使用LiveData,但是Firestore中的更改会立即反映出来(我相信这是Cloud Firestore的许多好处之一).

It did not even use LiveData but the changes in Firestore were reflected instantly (which I believe is one of many benefits of Cloud Firestore).

不需要任何LiveData对象,因为实时的所有数据获取工作都是由库完成的.是的,Cloud Firestore是一个实时数据库,但是您无需使用库也可以实现相同的行为.您可以使用Cloud Firestore客户端SDK,但是所有这些机制都应由您自己实现.因此,实时机制实际上是由Firebase-UI库提供的,而不是由Cloud Firestore数据库本身提供的.要实时收听更改,请查看有关成为现实的官方文档Cloud Firestore进行实时更新:

There is no need for any LiveData object, as all the work for getting the data in real-time is accomplished by the library. Yes, Cloud Firestore it's a real-time database, but you can achieve the same behavior also without the use of a library. You can use Cloud Firestore client SDK, but all that mechanism should be implemented by yourself. So that real-time mechanism was actually provided by the Firebase-UI library and not by the Cloud Firestore database itself. To listen for changes in real-time, please check the official documentation regarding, Get real-time updates with Cloud Firestore:

您可以使用onSnapshot()方法收听文档.使用您提供的回调进行的初始调用会立即使用单个文档的当前内容创建一个文档快照.然后,每次内容更改时,另一个调用都会更新文档快照.

You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document. Then, each time the contents change, another call updates the document snapshot.

此外,

但是现在在这个我要实现体系结构的项目中,我遇到了ViewModel类的问题.

But now in this project where I want to implement the architecture, I am having an issue with the ViewModel class.

据我了解,您想在项目中实现MVVM体系结构模式.在这种情况下,您将必须为此编写代码,这意味着您应该通过分别创建模型,视图(活动或片段)和ViewModel来分离关注点.

As I understand, you want to implement the MVVM architecture pattern in your project. In this case, you'll have to write code for that, meaning that you should separate the concerns, by creating a Model, a View (activity or fragment), and a ViewModel separately.

如何将FirestoreRecyclerOptions与ViewModel一起使用?

And how do I use the FirestoreRecyclerOptions with the ViewModel?

您不能将Firebase-UI库与MVVM架构模式混合使用,因为它们都不相互兼容.是一个或另一个.

You cannot mix the Firebase-UI library with the MVVM architecture pattern, as none of them is compatible with each other. It's one, or the other.

还是我完全需要LiveData?

Or do I need the LiveData at all?

是的,您需要一个LiveData对象,该对象可以直接在Activity/fragment类的ViewModel类中观察到.

Yes, you need a LiveData object that can be observed in the ViewModel class directly from the activity/fragment class.

参考文档指出,我应该在某些对象或集合上使用MutableLiveData,然后应该有一种方法可以在每次数据更改时更新列表.还有一个观察者方法可以实现.

The reference docs state that I should use MutableLiveData on some object or collection, and then I should have a method to update the list every time the data changes. There is also an observer method that is implemented.

是的,这就是MVVM的工作原理.

Yes, that's how MVVM works.

但是我没有看到有关Firestore的任何解释,也没有看到FirestoreRecyclerAdapter的工作原理.

But I don't see any explanation about Firestore or how the FirestoreRecyclerAdapter is even working.

您看不到对"FirestoreRecyclerAdapter"的任何使用;因为您不能在MVVM体系结构模式中使用这种对象.发生这种情况的原因是,当创建"FirestoreRecyclerAdapter"的新实例时,会创建"FirestoreRecyclerAdapter".类,您需要一个"FirestoreRecyclerOptions"应该传递给其构造函数的对象.这意味着此类对象与活动/片段紧密相关.因此,如果您在活动类中使用此类对象,则会违反MVVM体系结构模式原则,该原则表示活动对数据源应该一无所知.

You cannot see any use of "FirestoreRecyclerAdapter" because you cannot use this kind of object in an MVVM architecture pattern. This is happening because when creating a new instance of the "FirestoreRecyclerAdapter" class, you need a "FirestoreRecyclerOptions" object that should be passed to its constructor. This means that this kind of object is tight to the activity/fragment. So if you are using this kind of object in the activity class, you break the MVVM architecture pattern principle, which says that the activity should know nothing about its data source.

我需要在ViewModel类中实现关于数据库集合上MutableLiveData的实现,并在Cloud Firestore的上下文中更新FirestoreRecyclerAdapter中的功能.

What I need is what do I need to implement in the ViewModel class in respect to MutableLiveData on collections from the database and update functions in the FirestoreRecyclerAdapter in the context of Cloud Firestore.

如上所述,就MVVM架构模式而言,您不能这样做.是一个或另一个.您可以使用Firebase-UI库及其附带的所有优点,也可以使用MVVM架构模式.

As explained above you cannot do that, with respect to the MVVM architecture pattern. It's one or the other. You can use the Firebase-UI library with all the benefits it comes with, or you can use the MVVM architecture pattern.

您可能想知道,哪个更好?

You might wonder, which is better?

这取决于您认为更重要的东西,库或体系结构模式的好处.如果您要问我,我希望使用简化的代码并使用该库.

It depends on what you consider more important, the benefits of the library or the architecture pattern. If you are asking me, I prefer to have a simplified code and use the library.

如果您想要简单的代码,则可以使用通过Hilt进行依赖注入,然后注入"FirestoreRecyclerOptions"实例类,直接进入您的活动/片段类.

If you want a simpler code, then you can use Dependency injection with Hilt, and inject an instance of your "FirestoreRecyclerOptions" class, directly into your activity/fragment class.

请参阅下面的我的一个存储库,作为使用MVVM从Firestore获取数据的示例:

Please see below one of my repositories, as an example of getting data from Firestore using MVVM:

这篇关于在使用Cloud Firestore后端实施ViewModel类时需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆