0

I'm using NSfetchedResultsController.

I need to send an update to CloudKit when all controllerDidChangeContent methods finished. In my case controllerDidChangeContent method called several times, because I receive updates of every record in my tableView from CloudKit, so if I put a call to cloudKit update in controllerDidChangeContent, it is called for every row update. It is bad.

Is it possible to detect when controllerDidChangeContent finished all updates and put my CloudKit Send message inside?

Or maybe there is another method for this?

Adelmaer
  • 2,209
  • 3
  • 22
  • 45

1 Answers1

0

There's no good way to find out when all of the updates have finished, because there's no meaningful definition of "all of the updates". You can make new updates at any time, so there could always be a new update ready for the fetched results controller.

To see when updates to a specific list of objects have finished, you might be able to do something like

  1. Make a list of object IDs that you will update
  2. Every time the fetched results controller receives an update, remove the object IDs for the affected objects from the list.
  3. When the list is empty, you're done with that group of updates.

For a more general solution you could use an NSTimer to trigger the update only when no new updates have been received for a few seconds. The steps would be:

  1. In your controllerDidChangeContent, check if the timer exists. If not, create one.
  2. Set the timer's fireDate to be 5 seconds in the future (or however many you want).

You'll keep delaying the timer 5 seconds into the future every time you get an update. If 5 seconds pass with no updates, the timer will fire.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170