无法观察实时数据

时间:2019-07-22 17:02:21

标签: java android

我正在将一个空值传递给我的图像识别服务。我对为什么感到困惑,但是我的编译器似乎只是“跳过”了必要的实时数据。

我尝试使用lambda来解决此问题,广告已将布尔值添加到调用触发观察者的方法中。

这是我片段中有问题的代码(添加了日志,因为调试器在[{"PROJECT"=>"awesome_project1","VERSION"=>[128,64], "STATUS"=>["not required","scheduled"]}, {"PROJECT"=>"awesome_project2", "VERSION"=>32, "STATUS"=>"finished"}] 处失去了空间)

viewModel.getSandwichForModel()

这是viewModel中的方法:

 viewModel.pruneTutorial().observe(this, (completed) -> {
        Log.d(getClass().getName(), "Observe prune tutorial" + completed.toString());
        if (completed) {
          Log.d(getClass().getName(), "Observe if completed" + completed.toString());
          viewModel.getSandwichForModel().observe(this, (sandwiches) -> {
            Log.d(getClass().getName(), "Observe sandwich for model" + sandwiches.toString());
            ClarifaiService.ClarifaiPutImagesInModel modelMaker = new ClarifaiService.ClarifaiPutImagesInModel();
            modelMaker.execute(sandwiches.toArray(new Sandwich[0]));
            Log.d(getClass().getName(), "Observe sandwich for model" + sandwiches.toString());
          });
        }
      });

以下是道中的后续方法

 public LiveData<Boolean> pruneTutorial() {
    if (pruneResult == null) {
      pruneResult = new MutableLiveData<>();
    }
    new Thread(() -> {
      SandyDatabase db = SandyDatabase.getInstance(getApplication());
      db.sandwichDao().tutorialDelete();
      pruneResult.postValue(true);
    }).start();
    return pruneResult;
  }


  public LiveData<List<Sandwich>> getSandwichForModel() {
    SandyDatabase db = SandyDatabase.getInstance(getApplication());
    return db.sandwichDao().getAllNotResource();
  }

最后,这是ClarifaiService子类:

String PRUNING_QUERY = 
    "DELETE FROM "
    + "    Sandwich "
    + "WHERE "
    + "    sandwich_style IN ( "
    + "        SELECT "
    + "            sandwich_style "
    + "        FROM  "
    + "            Sandwich  "
    + "        WHERE  "
    + "            sandwich_id <= 12  "
    + "            AND NOT human_eat  "
    + "            AND sandwich_id NOT IN (4, 8, 12) "
    + "    )";

  @Query("SELECT * FROM sandwich WHERE NOT image_resource")
  LiveData<List<Sandwich>> getAllNotResource();

  @Query(PRUNING_QUERY)
  int tutorialDelete();

我希望通话成功,或者至少写了所有Log。我和我的教授都很困惑。相反,这就是LogCat中的内容:

 public static class ClarifaiPutImagesInModel extends
      AsyncTask<Sandwich, Void, ClarifaiResponse<List<ClarifaiInput>>> {


    @Override
    protected ClarifaiResponse<List<ClarifaiInput>> doInBackground(Sandwich... sandwiches) {
      List<ClarifaiInput> inputs = new LinkedList<>();
      for (Sandwich sandwich : sandwiches) {
        ClarifaiInput input = ClarifaiInput.forImage(sandwich.getFileName())
            .withConcepts(Concept.forID("sandwich"));
        inputs.add(input);
      }
      return client.addInputs()
          .plus(inputs)
          .executeSync();
    }

    @Override
    protected void onPostExecute(ClarifaiResponse<List<ClarifaiInput>> response) {
      if (response.isSuccessful()) {
        new ClarafaiCreateModel().execute();
      } else {
       //TODO figure out how to show a snackbar/toast without reference to a view
           }
        }

  }

0 个答案:

没有答案