title: How to check if an item already exists in a Room Database?
body: <p>I have this adapter with a button that dependent on if a object already exists in my Room Database or not will have a differente behavior when it´s clicked. Basically  what I want to do is  if the object exists I want to remove it. In case it doesn´t I want to add it to my database. I created this method in Dao and a Task to   check the existence. Since the Task is asynchronous ,how can I do the verification?</p>
  <h2>My Adapter</h2>
  <pre><code>public class RestaurantAdapter extends RecyclerView.Adapter&lt;RestaurantAdapter.RestaurantViewHolder&gt; {
      private Context mContext;
      private List&lt;Restaurant_&gt; mRestaurants;
      private Activity act;
      private FirebaseAuth mAuth;
      private String currentUserId;
      private View rView;
  
      public RestaurantAdapter(Context context, List&lt;Restaurant_&gt; restaurants, Activity activity) {
          mRestaurants = restaurants;
          mContext = context;
          act = activity;
      }
  
      @Override
      public RestaurantViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          // Get layout inflater from context
          Context context = parent.getContext();
          LayoutInflater inflater = LayoutInflater.from(context);
  
          // Inflate layout
          rView = inflater.inflate(R.layout.item_restaurant, parent, false);
  
          // Return a new holder instance
          return new RestaurantViewHolder(rView);
      }
  
      @Override
      public void onBindViewHolder(RestaurantViewHolder viewHolder, final int position) {
  
          // Get the data model based on position
          final Activity activity = act;
  
          final Restaurant_ restaurant = mRestaurants.get(position);
  
          mAuth = FirebaseAuth.getInstance();
          currentUserId = mAuth.getCurrentUser().getUid();
  
          final TextView name = viewHolder.nameTextView;
          name.setText(restaurant.getName());
  
          final TextView rating = viewHolder.ratingTextView;
          rating.setText((restaurant.getUserRating().getAggregateRating()));
  
          final TextView distance = viewHolder.distanceTextView;
          distance.setText(String.valueOf(restaurant.getDistance()) + &quot; Km&quot;);
  
          final ImageButton addToWishlistButton = viewHolder.addToWishlistButton;
          addToWishlistButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Wishlist wishlist = new Wishlist(currentUserId, restaurant.getId());
                  //if(alreadyExists){
                  //RemoveWLTask rlt=new RemoveWLTask(wishlist,activity);
                  //rlt.execute()
                 // }
  
                 // else{
                      AddWLTask wlt = new AddWLTask(wishlist, activity);
                      wlt.execute();
                  //}
              }
          });
  
          final ImageButton addToFavoritesButton = viewHolder.addToFavoritesButton;
          addToFavoritesButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  addToFavoritesButton.getBackground().setTint(activity.getResources().getColor(R.color.red));
                  addToFavorites();
              }
          });
  
          viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  LiveFragment.getListener().onRestaurantClicked(restaurant.getId());
              }
          });
      }
  
      @Override
      public int getItemCount() {
          return mRestaurants.size();
      }
  
  
      public class RestaurantViewHolder extends RecyclerView.ViewHolder {
          public TextView nameTextView;
          public TextView ratingTextView;
          public TextView distanceTextView;
          public ImageButton addToWishlistButton;
          public ImageButton addToFavoritesButton;
  
          public RestaurantViewHolder(View itemView) {
              super(itemView);
              nameTextView = itemView.findViewById(R.id.restaurantName);
              ratingTextView = itemView.findViewById(R.id.restaurantRating);
              distanceTextView = itemView.findViewById(R.id.restaurantDistance);
              addToWishlistButton = itemView.findViewById(R.id.button_wishlist);
              addToFavoritesButton = itemView.findViewById(R.id.button_favorites);
          }
  
      }
  
  }
  </code></pre>
  <h2>My DAO</h2>
  <pre><code>@Dao
  public interface DAO {
      @Insert
      public void addToWishlist(Wishlist wishlist);
  
      @Delete
      public void deleteFromWishlist(Wishlist wishlist);
  
      @Query(&quot;Select restaurantId From wishlist Where userId=:id&quot;)
      public String[] loadWishlist(String id);
  
      @Query(&quot;Select restaurantId From wishlist where userId=:userID AND restaurantId=:restaurantID&quot;)
      public String[]checkExists(String userID, String restaurantID);
      
  }
  </code></pre>
  <p>##My Task ##</p>
  <pre><code>public class CheckWLTask extends AsyncTask&lt;Void, Void, Void&gt; {
      private DB db;
      private Activity activity;
      private String userId;
      private String restaurantId;
      private String [] response;
  
      public CheckWLTask(Activity activity, String idUser, String idRestaurant) {
          this.activity = activity;
          this.userId = idUser;
          this.restaurantId = idRestaurant;
          db = Room.databaseBuilder(activity.getApplicationContext(), DB.class, &quot;sample-db&quot;).build();
      }
  
      @Override
      protected Void doInBackground(Void... voids) {
          while (!isCancelled()) {
              this.response=db.daoAcess().checkExists(userId,restaurantId);
              break;
          }
          return null;
      }
  
  
  }
  ``
  
  </code></pre>