Search code examples
macoscocoafsevents

How to use kFSEventStreamEventFlagEventIdsWrapped with FSEvents?


I'm trying to understand how to use the kFSEventStreamEventFlagEventIdsWrapped event flag with FSEvents.

According to the documentation, the flag is sent to registered instances when the event id counter wraps around, thus rendering previous event id obsolete.

Now let's imagine the following scenario:

  • I register for FSEvents in my application;
  • When done processing FSEvents (my application quits for instance), I save the last event id encountered while processing events to be able to replay changes from that id;
  • While my application is not running, the event id counter wraps around.

My question is: How am I supposed to know the counter wrapped around? (Thus requiring me to re-scan the whole directory structure.)


Solution

  • I now have an answer directly from Apple.

    The scenario was wrong to begin with. When saving the last event id treated, you must also save with it the UUID of the event stream. An event id is valid only for a given event stream, which is identified by its UUID (see FSEventsCopyUUIDForDevice()).

    Whenever the event id counter wraps around, a new event stream UUID is generated. So if you relaunch the app after the event id counter wrapped around, your stored last event id won’t be valid anymore, and you’ll know it as the event stream UUID won’t be the same.

    If you encounter the kFSEventStreamEventFlagEventIdsWrapped flag, it means the counter wrapped around while your app was open. However, there’s nothing particular to be done. You should just be sure to get the new event stream UUID if you want to save the last event id.