Android 您的方法是,如果返回LiveData,Room显式地允许在主线程上执行此类操作,那么这意味着将对我的存储库的所有调用封装在某个asynctask或类似的内容中。在阅读了你的问题中的所有评论/聊天信息后,我发现这很奇怪,这个房间的行为方式与你的想法相同,即使

Android 您的方法是,如果返回LiveData,Room显式地允许在主线程上执行此类操作,那么这意味着将对我的存储库的所有调用封装在某个asynctask或类似的内容中。在阅读了你的问题中的所有评论/聊天信息后,我发现这很奇怪,这个房间的行为方式与你的想法相同,即使,android,observable,android-room,android-architecture-components,android-livedata,Android,Observable,Android Room,Android Architecture Components,Android Livedata,您的方法是,如果返回LiveData,Room显式地允许在主线程上执行此类操作,那么这意味着将对我的存储库的所有调用封装在某个asynctask或类似的内容中。在阅读了你的问题中的所有评论/聊天信息后,我发现这很奇怪,这个房间的行为方式与你的想法相同,即使你认为是这样,如果真的没有其他选择,我也会接受你的答案并这样做。但是也许有人可以更清楚地了解这个问题,并找到一种直接从dao返回LiveData的方法:)我也很喜欢这种方法,因为这种方式有点奇怪:)当其他服务更改了DB值时,这种方法会更新UI吗


您的方法是,如果返回LiveData,Room显式地允许在主线程上执行此类操作,那么这意味着将对我的存储库的所有调用封装在某个asynctask或类似的内容中。在阅读了你的问题中的所有评论/聊天信息后,我发现这很奇怪,这个房间的行为方式与你的想法相同,即使你认为是这样,如果真的没有其他选择,我也会接受你的答案并这样做。但是也许有人可以更清楚地了解这个问题,并找到一种直接从dao返回LiveData的方法:)我也很喜欢这种方法,因为这种方式有点奇怪:)当其他服务更改了DB值时,这种方法会更新UI吗?因为是的,如果您只在调用存储库的get(…)方法时刷新值,这是没有问题的,但是如果例如ContentProvider在db中插入了一些内容,我认为在下次调用get(…)之前,UI不会显示更改。在这里谈论你的链接问题代码你可能是对的,但我遵循了中的示例,这就是他们在那里解释的。根据链接中给出的信息,我认为我没有做错任何事情。我认为谷歌提供的信息在这一点上是不够的,也许因为这仍然是alphaYep,这就是答案。@更新工作,@Query与更新查询一起工作。只是不同的数据库实例导致了这些问题。请参阅,以获取一篇关于Singleton(房间数据库需要上下文)的优秀kotlin文章,这篇文章是您拯救了我的一天
  @Test
  public void shouldSaveFormsFromServerIntoDb() throws Exception
   {
    Lifecycle lifecycle = Mockito.mock(Lifecycle.class);
    when(lifecycle.getCurrentState()).thenReturn(Lifecycle.State.RESUMED);
    LifecycleOwner owner = Mockito.mock(LifecycleOwner.class);
    when(owner.getLifecycle()).thenReturn(lifecycle);

    final CountDownLatch l = new CountDownLatch(19);

    formRepository.allForms().observe(owner, formList ->
    {
     if (formList != null && formList.isEmpty())
      {
       for (Form form : formList)
        {
         testForm(form);
         l.countDown();
        }
      }
    });

    formRepository.synchronizeFormsWithServer(owner);
    l.await(2, TimeUnit.MINUTES);
    assertEquals(0, l.getCount());
   }
  @Override
  public LiveData<List<Form>> allForms()
   {
    return formDatastore.getAllForms();
   }
  @Override
  public LiveData<List<Form>> getAllForms()
   {
    return database.formDao().getAllForms();
   }
  @Query("SELECT * FROM form")
  LiveData<List<Form>> getAllForms();
@Update
void update(Form form)
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Form form);
LiveData<List<Form>> liveData = formRepository.allForms();
  @Test
  public void shouldSaveFormsFromServerIntoDb() throws Exception
   {
    Lifecycle lifecycle = Mockito.mock(Lifecycle.class);
    when(lifecycle.getCurrentState()).thenReturn(Lifecycle.State.RESUMED);
    LifecycleOwner owner = Mockito.mock(LifecycleOwner.class);
    when(owner.getLifecycle()).thenReturn(lifecycle);

    final CountDownLatch l = new CountDownLatch(19);

    final SortedList<Form> sortedForms = new SortedList<Form>(Form.class, new SortedList.Callback<Form>()
     {
      @Override
      public int compare(Form o1, Form o2)
       {
        return o1.getUniqueId().compareTo(o2.getUniqueId());
       }


      @Override
      public void onChanged(int position, int count)
       {
        Log.d(LOG_TAG, "onChanged: Form at position " + position + " has changed. Count is " + count);
        for (int i = 0; i < count; i++)
         {
          l.countDown();
         }
       }


      @Override
      public boolean areContentsTheSame(Form oldItem, Form newItem)
       {
        return (oldItem.getContent() != null && newItem.getContent() != null && oldItem.getContent().equals(newItem.getContent())) || oldItem.getContent() == null && newItem.getContent() == null;
       }


      @Override
      public boolean areItemsTheSame(Form item1, Form item2)
       {
        return item1.getUniqueId().equals(item2.getUniqueId());
       }


      @Override
      public void onInserted(int position, int count)
       {

       }


      @Override
      public void onRemoved(int position, int count)
       {

       }


      @Override
      public void onMoved(int fromPosition, int toPosition)
       {

       }
     });

    LiveData<List<Form>> ld = formRepository.allForms();
    ld.observe(owner, formList ->
    {
     if (formList != null && !formList.isEmpty())
      {
       Log.d(LOG_TAG, "shouldSaveFormsFromServerIntoDb: List contains " + sortedForms.size() + " Forms");
       sortedForms.addAll(formList);
      }
    });

    formRepository.synchronizeFormsWithServer(owner);
    l.await(2, TimeUnit.MINUTES);
    assertEquals(0, l.getCount());
   }