草庐IT

mongo-livedata

全部标签

android - 使用Android架构组件LiveData&ViewModel时是否需要使用onSaveInstanceState和onRestoreInstanceState?

AndroidArchitectureComponents提供LiveData和ViewModel类,它们对生命周期更友好,专为更精简的Activity/Fragment而设计。这些类处理跨配置更改的数据存储,但与Activity框架API相比,我对它们的使用感到困惑。onSaveInstanceState(Bundle)和onRestoreInstanceState(Bundle)对于保存Activity状态是否仍然必要或有用? 最佳答案 onSaveInstanceState和onRestoreInstanceState仍然有用

android - 尝试在空对象引用上调用虚方法 'void android.arch.lifecycle.LiveData.observe

我收到以下错误我正在使用Android架构组件并尝试实例化View模型并从LiveData观察数据。但我被困在这里请帮我解决这个问题:Attempttoinvokevirtualmethod'voidandroid.arch.lifecycle.LiveData.observe(android.arch.lifecycle.LifecycleOwner,android.arch.lifecycle.Observer)'onanullobjectreference我的fragment是这样的:在调用getCompany()方法时我得到了NullPointExceptionErrorpub

android - 未观察到对设置 LiveData 的多次调用

我最近看到一个奇怪的问题阻碍了我的项目。设置实时数据值的多次调用不会在View中调用观察者。似乎只有最后设置的值才真正调用View中的观察者。这是评论的代码fragment。主Activity.ktclassMainActivity:AppCompatActivity(){privatelateinitvarviewModel:MainViewModeloverridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.activity_mai

android - EditText LiveData 双向绑定(bind)

好吧,我有一个ViewModel带setter/gettergetTitle()返回MutableLiveData.一开始效果很好:EditText包含MutableLiveData的值当它第一次出现时。但是,如果使用MutableLiveData.setValue()更新此数据的值(例如通过另一个EditText,或来self的代码),那么文本框内的值不会改变。我该如何解决这个问题? 最佳答案 这在newversionofAndroidStudio中正常工作,它支持正确绑定(bind)到LiveData对象。

android - 具有多个参数的 LiveData Transformations.map()

我在UI中有一个值,它的值取决于两个LiveData对象。想象一家商店,您需要subtotal=sumofallitemsprice和total=subtotal+shipmentprice。使用Transformations我们可以对小计LiveData对象执行以下操作(因为它仅取决于itemsLiveData):valitemsLiveData:LiveData>=...valsubtotalLiveData=Transformations.map(itemsLiveData){items->getSubtotalPrice(items)}在总数的情况下,能够做这样的事情会很棒:v

android - 使用 LiveData 检查 RoomDatabase 是否为空

我正在尝试在我的Android应用程序中使用RoomDatabase。我正在使用LiveData来自动刷新fragment中的更改。我第一次运行我的应用程序时,我从API获取数据,创建我的RoomDatabase并存储我的数据。第二次运行我的应用程序时,我想检查我的数据库是否不为空。但是在使用LiveData时:以下代码返回null。AppDatabase.getInstance(getContext()).getRecipeDao().getAllRecipes().getValue();我读到“如果响应是可观察的数据类型,例如Flowable或LiveData,Room会监视查询中

mongo数据备份

目录1.mongo单机安装2.mongo(replicaset)部署1.无密码副本集部署2.单机密码认证mongo改造单点Primary3.mongodump与mongorestore工具使用4.rsync工具使用服务端配置客户端配置客户端推送与拉取文件5.完整mongo全量备份脚本恢复全量备份数据6.完整mongo增量备份脚本(基于oplog)恢复增量备份数据7.备份策略1.mongo单机安装mongo版本:4.4.19服务器版本:CentOSLinuxrelease7.9.2009cd/etc/yum.repos.d/touchmongodb-org-4.4.repo[mongodb-or

android - 我们如何在 ViewModel 中将 "assign"LiveData 从 Room 转移到 MutableLiveData

最近,我被下面的代码困住了。publicclassNoteViewModelextendsViewModel{privatefinalMutableLiveData>notesLiveData=newMutableLiveData();publicNoteViewModel(){LiveData>notesLiveDataFromRepository=NoteRepository.INSTANCE.getNotes();//HowcanI"assign"LiveDatafromRoom,toMutableLiveData?}}我想知道,如何将Room中的LiveData“分配”到Mut

android - 如何在不使用数据绑定(bind)的情况下让 EditText 观察 ViewModel 的 LiveData 并将用户输入转发到 ViewModel

我正在尝试想出一种方法让EditText更新ViewModel的数据并同时观察该数据的任何更改(例如,通过操作数据库带来的更改)。有没有办法在不使用数据绑定(bind)库的情况下做到这一点?我在简单地使用MutableLiveData时面临的主要问题如下:当用户在EditText中输入文本时,TextWatcher会触发ViewModel以更新其数据,这反过来会将新文本设置到MutableLiveData对象。因为EditText正在观察LiveData,所以会触发onChange并相应地设置EditText的文本,这又会再次触发TextWatcher,从而形成无限循环。

Android MVVM - 如何让 LiveData 发出它拥有的数据(强制触发观察者)

我有一个从网络获取列表的ViewModel,我用数据填充了一个RecyclerView(MyAvailabilityRepository返回一个MutableLiveData,这就是我使用Transformations.switchMap的原因):classMyAvailabilityViewModel:ViewModel(){privatevalgetListsParams=MutableLiveData()privatevalgetListsObservable=Transformations.switchMap(getListsParams){organizationId->My