timbroder
timbroder

Reputation: 878

Hold or batch Realm notifications?

I have a scenario where I'll be doing many updates that end up effecting a Result that my VC is listening to. Ideally I'd just send 1 notification at the end of my batch

A few questions:

  1. Is it possible to batch notifications, rather than having every update send a notification?
  2. Is this even the right way to think about it? Should I just stop listening to the notifications when the batch starts and start listening again when it's done?

Upvotes: 1

Views: 326

Answers (1)

TiM
TiM

Reputation: 15991

Notifications only occur at the end of a write transaction, so the easiest thing to do for this case would be to perform all of your work in one transaction (I'd definitely recommend doing it on a background thread), and then close it when you want the notification to be fired.

This would be the general recommended approach since it would guarantee all fine-grained change indexes are coalesced into that singular notification at the end.

If you don't care about the fine-grained change indexes (i.e., you're just doing a complete refresh on each notification), then you definitely could consider simply setting a flag that disregards notifications until you've completed your work.

You could also remove the notification block, but this would mean there'd be a significant amount of tear-down, and re-setup work you'd have to do each time.

Upvotes: 3

Related Questions