WatermelonDB icon indicating copy to clipboard operation
WatermelonDB copied to clipboard

Observe ids

Open zhirzh opened this issue 2 years ago • 0 comments

Discussed in https://github.com/Nozbe/WatermelonDB/discussions/1381

Originally posted by zhirzh August 28, 2022 An observable equivalent to fetchIds that emits ids when records are added or removed. This behaviour can be achieved by chaining observeCount with fetchIds.

class Query {
  ...
  observeIds(): Promise<RecordId[]> {
    return this.observeCount().pipe(switchMap(() => this.fetchIds()))
  }
}

Note that this implementation has followingcaveats:

  • requires 2 database queries and data transfers over bridge
  • no value is emitted if number of matching records remains unchanged even though matching records changed

zhirzh avatar Aug 30 '22 14:08 zhirzh