CoreDataBestPractices icon indicating copy to clipboard operation
CoreDataBestPractices copied to clipboard

Request for additional example code

Open jbehrens94 opened this issue 3 years ago • 5 comments

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 avatar Feb 01 '21 12:02 jbehrens94

@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.

AvdLee avatar Feb 02 '21 09:02 AvdLee

So, maybe you'd throw those in as parameters to the startObservingPersistentHistoryTransactions() method? And then have a separate PersistentContainer for every target?

jbehrens94 avatar Feb 03 '21 07:02 jbehrens94

Yeah, there are many ways of doing this actually, maybe even initialise the AppTarget based on a raw string matching the bundle identifier

AvdLee avatar Feb 03 '21 13:02 AvdLee

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 avatar Feb 03 '21 14:02 jbehrens94

@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 🙂

AvdLee avatar Feb 04 '21 09:02 AvdLee