开发者社区> 问答> 正文

房间观察员的速度不够快吗?我可以等待onChange执行吗?

我在Andrid Studio中编写程序,并且将Room与Sqlite一起使用。我有一个Entity,以及相应的Dao。

谈论具有两列键和值的表键。在道我有

public interface KeyDao {

@Insert
void insert(Key key);

@Update
void update(Key key);

@Delete
void delete(Key key);

@Query("DELETE FROM key_table")
void deleteAllkeys();

@Query("SELECT * FROM key_table ORDER BY id DESC")
LiveData<List<Key>> getAllkeys();}

在onCreate的主要活动中,我做了两件事:

我得到所有表行 我设置了关于行的片段 为了获取所有行,我们设置了一个观察器并检测onChange

keyViewModel = ViewModelProviders.of(this).get(KeyViewModel.class);
    keyViewModel.getAllKeys().observe(this, new Observer<List<Key>>() {
        @Override
        public void onChanged(@Nullable List<Key> all_key_rows) {
            Giveme_all_the_rows = all_key_rows;

        }
    });

为了显示Fragment,我们调用FragmentManager

if (savedInstanceState == null) {
 // If some rows do this BUT I FIND ROWS EMPTY!!
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new MyFragment()).commit();

现在,这两个任务在Activity onCreate(Bundle savedInstanceState)中

我想在声明onChange之后立即阅读Giveme_all_the_rows变量,但发现它为空,我想立即阅读以显示关于某些行的Fragment

展开
收起
几许相思几点泪 2019-12-16 20:28:53 442 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载