Core Data and NSOperation

后端 未结 4 903
情话喂你
情话喂你 2021-02-04 19:49

I\'m currently working with an NSPersistentDocument subclass that uses NSOperation to import data in the background. As per the documentation, I\'m obs

相关标签:
4条回答
  • 2021-02-04 20:07

    I'm nobody's Core Data expert, but from what I can tell from the docs, you'll want to start with an in-memory store until the user (in their own time) saves the document. Then, send the coordinator a migratePersistentStore:toURL:options:withType:error: message to change over from the in-memory store to the new truly-persistent store. See that document for some essential details (particularly regarding the fate of the store you migrate).

    0 讨论(0)
  • 2021-02-04 20:15

    My first thought on the workflow/saving part would be, if a persistent store hasn't yet been created for the document, to create a temporary in-memory store, so that the imported data would be saved to that store instead (though the document/window would still be marked as dirty). Then, once the user saves the document for real, you would reconfigure the coordinator to remove the in-memory store and replace it with the on-disk store, so all further saves would go to disk.

    0 讨论(0)
  • 2021-02-04 20:20

    I'm not 100% familiar with the Mac stuff, but I'm sure you could you use an in-memory persistent store before the user has saved, and then add the sql/plist store after that action.

    Potentially even better could be to create a on-disk persistent store in a standard temporary directory, and move it across when the user clicks to save.

    0 讨论(0)
  • 2021-02-04 20:21

    Have you tried setting up a temporary file URL when setting up the coordinator? You should be able to undo the -mergeChangesFromContextDidSaveNotification: on the main thread. No need to register an undo manager for the MOC on the background thread.

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