Auto-save with Cocoa and Core Data

前端 未结 1 748
温柔的废话
温柔的废话 2021-02-04 10:06

I am working on a non-document-based Core Data application.

I would like changes to be saved as they happen. This is what the user expects in this type of application. I

1条回答
  •  长发绾君心
    2021-02-04 11:01

    At the end of each event in an AppKit app, CoreData will run a -processPendingTransactions for you.

    One side-effect of this is that if you've registered with your NSManagedObjectContext to receive change notifications, you'll get called at the end of each event.

    So, for instance, in your notification handler, you could call just tell the context to save.

    However, you might be paranoid about doing a save on a context while in a callback from that same context, so you'd probably feel better if you did a performSelector:@selector(save:) afterDelay: to push the save until after the -processPendingTransactions is done.

    You could even do a cancel previous on the -save: selector and have the delay be like 5 seconds, so if the user or app is in the middle of a BUNCH of changes they'll all coalesce into a single save.

    And, in fact, that's exactly how Delicious Library 1.0-1.09 worked.

    -Wil

    0 讨论(0)
提交回复
热议问题