0

I am continuously receiving updates from a server and placing them in my ObjectBox database. My app is meant to visualize this data. The data involves runners, competition classes, etc.

The effect I want to achieve is that when there is an update to a class of runners, the widget visualizing this class (competition class - not programming class) knows it's time to request data and redraw. It seems to me that this might be accomplished by listening to a query that filters runners by class.

@Entity()
class Runner {
  int id;
  String name;
    
  final runnerClass = ToOne<Class>();
  // constructors, other fields and methods omitted
}

@Entity()
class Class {
  int id;
  String name;

  @Backlink("runnerClass")
  final runners = ToMany<Runner>();
  // constructors, other fields and methods omitted
}

// in persistence class
late Stream<Query<Runner>> watchedRunners;

// in persistence class, method
QueryBuilder<Runner> query = runnerBox.query();
query.link(Runner_.runnerClass, Class_.name.equals("Easy"));
watchedRunners = query.watch();

// in listening class
final sub = db.watchedRunners.listen((Query<Runner> query) {
  print("THINGS BE HAPPENING");
});

I then test this by changing one runner on the server. I change their class from one that is not Easy to another that is not Easy. In other words, the runners in the Easy class receive no additions, removals or modifications, and in the XML update from the server, I can see as much. Yet this listener fires, I'm guessing because there was a change to runners at all.

If I print the results of the query in the listening class, it says everyone is in the Easy class; if I print the length it constantly says 31 (whereas there are 82 runners in total). If I actually move runners in/out of the Easy class, the number updates to reflect it, and the list of names checks out. So it seems to me the query is correct, but the listener is not.

  • I'm not sure how your code accesses data exactly (e.g. typically re-run the query in the Stream listener), but something to be aware of: ToMany caches its objects upon first access. To get the latest data, the object owning the ToMany has to be retrieved from the database again. – Uwe - ObjectBox Jun 15 '22 at 07:56
  • Yeah, I intend to access it that way, and thank you for the heads-up. But the problem is that while the query seems correct, the listener seems to be for the entire Runner box, not just those participating in the Easy class. – Albert Henmyr Jun 15 '22 at 16:56
  • Ah yes, observers in ObjectBox always react to changes on anything in a box. If a query involves multiple entities/boxes, it'll react to changes on each. See the implementation at https://github.com/objectbox/objectbox-dart/blob/aee6b141bac82868e78cb82e691f81b6e69a6650/objectbox/lib/src/native/query/builder.dart#L35-L45 – Uwe - ObjectBox Jun 20 '22 at 05:46

0 Answers0