You're writing an app where the model (as in MVC) is an object graph. Occasionally, you want to save this object graph to disk. What are some best practices to ensure that the write is consistent? (That is, how do you prevent the object graph from mutating in the middle of a serialization/write?)
In my app, whenever a write is triggered, I make a quick in-memory copy of my object graph (blocking the model thread in the meantime), serialize that copy in a background thread, and then write it out. But this doesn't seem like a very elegant solution, especially if the model is fairly large. I don't want my UI to freeze while the copy is happening.
I can imagine locking the object graph to prevent mutation in the middle of a disk write, but this would either require loss of user input, or maintenance of a pending operation queue for the duration of the write.
I can also imagine versioning the model and incrementing the version number with each mutation, but this would additionally require persisting the version of the graph that's being written out. If we don't want to do a full copy on write, we would have to trigger a copy each time a model object is mutated during a write. Seems like it could get fairly complex.
Where can I read more about strategies used to tackle this issue? What approach is generally used to handle this problem?