CoreDataBestPractices
CoreDataBestPractices copied to clipboard
Request for additional example code
Hi @AvdLee,
Thank you very much for this helpful repository and the great examples you gave. I was wondering if you could add more example code to demonstrate how you would setup the PersistentContainer + PersistentHistoryTracking for a second target, because if I'm correct, you don't demonstrate that yet.
I would guess you don't want every instance of PersistentContainer.shared
to have the .app
target such as in the example from your repository below:
func startObservingPersistentHistoryTransactions() -> PersistentHistoryObserver {
let observer = PersistentHistoryObserver(.app, userDefaults: .standard, persistentContainer: self)
viewContext.name = "view_context"
viewContext.transactionAuthor = "main_app_view_context"
observer.startObserving()
return observer
}
Would you compile different extensions per target for example, or build several PersistentContainer
instances per target?
@jbehrens94 indeed, you need another case just like .app
, for example: .shareExtension
.
The transaction author is something you want to consider giving a different name too but the target change is the most important one here.
So, maybe you'd throw those in as parameters to the startObservingPersistentHistoryTransactions()
method? And then have a separate PersistentContainer
for every target?
Yeah, there are many ways of doing this actually, maybe even initialise the AppTarget
based on a raw string matching the bundle identifier
Might it be useful to make that an example as well? :) I think it could help people quickly grasp that concept and work it out for themselves if a prominent CoreData blogger shows that.
@jbehrens94 yes, it's definitely valuable! I don't have the time to focus on this now but feel free to do so. I can find time to over a PR at least and give some feedback 🙂