我可以在观察者中添加观察者吗?我实际上尝试过,但内部观察者无法正常工作有时其中一个工作,有时没有一个工作

如何解决我可以在观察者中添加观察者吗?我实际上尝试过,但内部观察者无法正常工作有时其中一个工作,有时没有一个工作

我使用了 Android 架构组件(ViewModel、LiveData)并使用 Room 从使用 Repository 的本地 Sqlite 数据库获取数据。最外面的观察者就好了,只有内部观察者有问题。

查看代码:

    @Override
    public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);
        BranchDetailsViewModel branchDetailsViewModel = new BranchDetailsViewModel(application);

        Observer<Bank> bankObserver = new Observer<Bank>() {
            @Override
            public void onChanged(Bank bank) {
                if(bank!= null)
                    binding.tvBank.setText(bank.getBank());
            }
        };


        branchDetailsViewModel.getBranchById(branchIid).observe(this,new Observer<Branch>() {
            @Override
            public void onChanged(Branch branch) {

                branchDetailsViewModel.getBankById(branch.getBankId()).observe(getViewLifecycleOwner(),bankObserver);

                branchDetailsViewModel.getCityById(branch.getCityId()).observe(requireActivity(),new Observer<City>() {
                    @Override
                    public void onChanged(City city) {
                        if(city != null)
                        binding.tvCity.setText(city.getCity() + " cty");
                    }
                });

                branchDetailsViewModel.getDistrictById(branch.getDistrictId()).observe(requireActivity(),new Observer<District>() {
                    @Override
                    public void onChanged(District district) {
                        if(district != null)
                        binding.tvDistrict.setText(district.getDistrict());
                    }
                });

                branchDetailsViewModel.getStateById(branch.getStateId()).observe(requireActivity(),new Observer<State>() {
                    @Override
                    public void onChanged(State state) {
                        if(state!= null)
                        binding.tvState.setText(state.getState());
                    }
                });
            }
        });
    }

我尝试使用命名和匿名观察者。我不明白这里有什么问题。

有时它会抛出 NullPointerExceptions。有时无法更改区域设置。

04-09 15:48:45.182 2096-2113/com.appsbharti.bharatbankdetails E/SQLiteDatabase: Failed to open database '/data/data/com.appsbharti.bharatbankdetails/databases/bank_details.db'.
    android.database.sqlite.SQLiteException: Failed to change locale for db '/data/data/com.appsbharti.bharatbankdetails/databases/bank_details.db' to 'en_IN'.
        at android.database.sqlite.SQLiteConnection.setLocaleFromConfiguration(SQLiteConnection.java:386)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:218)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
        at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:804)
        at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:92)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:53)
        at androidx.room.SQLiteCopyOpenHelper.getWritableDatabase(SQLiteCopyOpenHelper.java:90)
        at androidx.room.RoomDatabase.inTransaction(RoomDatabase.java:476)
        at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.java:281)
        at com.appsbharti.bharatbankdetails.Daos.HistoryDao_Impl.insert(HistoryDao_Impl.java:45)
        at com.appsbharti.bharatbankdetails.Repositories.HistoryRepository$1.run(HistoryRepository.java:37)
        at androidx.room.TransactionExecutor$1.run(TransactionExecutor.java:45)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
     Caused by: android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 1802)
        at android.database.sqlite.SQLiteConnection.nativeExecute(Native Method)
        at android.database.sqlite.SQLiteConnection.execute(SQLiteConnection.java:548)
        at android.database.sqlite.SQLiteConnection.setLocaleFromConfiguration(SQLiteConnection.java:364)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:218) 
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804) 
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789) 
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 
        at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:804) 
        at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221) 
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:92) 
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:53) 
        at androidx.room.SQLiteCopyOpenHelper.getWritableDatabase(SQLiteCopyOpenHelper.java:90) 
        at androidx.room.RoomDatabase.inTransaction(RoomDatabase.java:476) 
        at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.java:281) 
        at com.appsbharti.bharatbankdetails.Daos.HistoryDao_Impl.insert(HistoryDao_Impl.java:45) 
        at com.appsbharti.bharatbankdetails.Repositories.HistoryRepository$1.run(HistoryRepository.java:37) 
        at androidx.room.TransactionExecutor$1.run(TransactionExecutor.java:45) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
        at java.lang.Thread.run(Thread.java:856) 

还有一个问题是,我不确定应该在 Fragment 中使用哪一个作为 Observer 的所有者,requireActivity()getActivity()(我收到警告说这可能返回 null,所以我没有使用它) 或 getViewLifecycleOwner()(获取片段的生命周期所有者对象)。

解决方法

如果您在 Fragment 内,则不能通过 this 进行观察,您应该通过 viewlifecycleOwner。在您的情况下,您正在设置一个观察者以从房间获取分支的值,然后根据分支,您的其他观察者被调用。

这意味着您的内部观察者将在您的 branch 观察者被调用后进行观察。

当您将观察者绑定到片段的生命周期时,观察者会按照片段本身的生命周期工作。

将它与生命周期所有者绑定应该可以,主要是因为 this 在片段的情况下不起作用。

,

实际上,接受 viewlifecycleOwnerthis。对于内部,您必须调用 YourFragmnent.this

我看到您没有调用任何函数来更新内部 LiveData 值。 并且你不应该包装嵌套的观察者,因为内部观察者会被调用几次,对应于外部观察者被调用的次数。

你只需要调用更新LiveData值的函数来触发内部观察者的onChanged回调

请阅读本文以避免多次消耗数据https://medium.com/androiddevelopers/livedata-with-snackbar-navigation-and-other-events-the-singleliveevent-case-ac2622673150

,
  1. 请使用 YouFragmentClassName.thisviewLifecycleOwner
  2. 添加日志以检查您是否收到了 onChange 回调。
  3. 如果您仍然得到空值,请检查您的 viewModel 代码。
,

好的,问题是我没有在 synchronized 块内获取 AppDatabase。

在 AppDatabse 类中,我更改了 newInsance 方法:

public static AppDatabase getInstance(final Context context) {
    dbInstance = buildDatabaseInstance(context);
    return dbInstance;
}

public static AppDatabase getInstance(final Context context) {
    if (dbInstance == null)
        synchronized (AppDatabase.class) {
            if (dbInstance == null)
                dbInstance = buildDatabaseInstance(context);
         }
//  dbInstance = buildDatabaseInstance(context);
    return dbInstance;
}

现在所有的观察者都运行顺利。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res