Pergunta

I'm building an iOS application that uses the event sourcing pattern; the idea is that you should be able to reconstruct the application state at any time by replaying a series of events.

Each event carries a UUID. The application's state is stored in a persistent Core Data object graph.

My particular implementation of event sourcing guarantees "at least once" delivery of events to the client. If it so happens that an event is delivered more than once, I need to ensure that these subsequent deliveries don't affect the object graph.

I would like to keep an event record – a set of event UUIDs that have already been processed. This way, I can check incoming events against the event record, and drop them if they've already been processed.

Where would you store the event record?

It needs to be 'synchronized' with the persisted Core Data graph. In particular this means:

  • if the Core Data object graph is deleted, so must the event record
  • the event record must be persisted in tandem with the Core Data object graph
Foi útil?

Solução

There is no reason you cannot store it in Core Data as well. Personally, if I was already using Core Data to store data then I would use it to store all of the data so that I have one persistence interface to work with and not trying to align multiple different interfaces.

Update 1

I see where you're taking it. So instead of sending containsObject to a set of UUIDs in memory, you suggest that I use a fetchRequest to check for the existence of an entity in the store with that UUID? Since this check has to be performed on every event that comes in, what can I do to ensure that it's performant?

Do a count instead of a fetch. If the count is >= 1 then you already have that event. If it is zero you do not. Performing a count against the persistent store is very fast.

Second, use a pre-created NSFetchRequest (and NSPredicate) and keep it between checks. That will decrease the amount of time it takes to build your query.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top