Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.62k stars 600 forks source link

withObservables: observeCount() does not register very fast changes in DB #1513

Closed maXXCZ1 closed 1 year ago

maXXCZ1 commented 1 year ago

Hello,

quick question, i can't figure out if this is some WM limitation, or if i did something wrong.

I have simple icon enhanced by some data, nothing fancy:

export const FavouriteItemIconEnhanced = withObservables(
  ["userId"],
  ({userId, item}) => {
    return {
      // isItemInFavourite: database
      //   .get("user_favourite_item")
      //   .query(
      //     Q.where("user_id", userId),
      //     Q.where("item_id", item.id),
      //     Q.where("_status", Q.notEq("deleted")),
      //   )
      //   .observeCount(),
      isItemInFavourite: database
        .get("user_favourite_item")
        .query(
          Q.where("user_id", userId),
          Q.where("item_id", item.id),
          Q.where("_status", Q.notEq("deleted")),
        ),
    };
  },
)(FavItemIcon);

My icon should have "on" state when there is user_favourite_model present, and "off" state when there is no model (if i delete it). I used first commented approach, but once i clicked really fast on the icon, the icon (observer) lagged behind and even after i deleted model in DB it still displayed count as 1. So in database there was no item, but isItemInFavourite injected 1 as prop inside my FavItemIcon. BUT! When i used simple "observer" like uncommented code, and then inside FavItemIcon checked length of Query everything is on point real time, no matter how fast i click icon (obviously, it is lazy, so not exactly real time, but everything will catch up within few hundred milliseconds max.).

Am i doing something wrong, or there is some limitation to observeCount() function so it can't work as fast?

radex commented 1 year ago

https://watermelondb.dev/Query.html count is throttled by default, but you can disable it

LukasMod commented 2 months ago

current link: https://watermelondb.dev/docs/Query#count-throttling