I’ve been wanting to revamp some old code that wasn’t performing as I like. I had come up with something a bit too complicated involving
NSOperationQueue, fetching remote data, parsing it all in the background, then saving that to my Core Data’s persistent store.
I always thought my solution a bit too complicated, and not sure it was entirely correct / robust.
I don’t know about you, but I regularly have 6-7 Google Chrome windows open with tons of tabs. I have some articles that sit there for months that I don’t want to forget about. One of which was written by Marcus Zarra, a prominent source of Core Data info. He made it look so easy.
I’ve come to enjoy Core Data as a framework. I think there is no other way at this point. And I’m sure I’ve only scratched the surface of what it can do. Along with mogenerator in your build pipeline, and especially the associated controller (
NSFetchedResultsController) this framework is indispensable.
I basically took Marcus Zarra’s post and extended it to allow for doing data model work in the background, and also for making “scratch pad” contexts. That is, consider editing forms but then the user hits “cancel”. No rollbacks needed. Just discard the “editor” context. Or, consider data imports that run in the background, and you are on a view controller using an editor context. You can even tell that editor context to update itself with those changes that occurred in the meantime.
Anyway, all a bit vague, so I’d like to just refer you to my repository that demonstrates what I’m talking about at http://github.com/horseshoe7
Please clone it and run it. So a search for
scenarioToExamine and change that value.
UPDATE: I just found and read this article by Florian Kugler. This solution above is an implementation of his so called “Stack #2”. I really want to have a solution that’s going to be versatile and fast, so expect my repository’s implementation to change to be “Stack #3”. Will update again after this happens.
UPDATE 2: So I found my solution. It’s in the Repo. It’s called
HSHybridThreeStack. It has Marcus Zarra’s asynchronous background saving context, it has Florian Kugler’s separate context for speedy importing, it has main thread editing contexts that can optionally keep themselves updated to changes resulting from these import contexts, and an API that should be pretty straightforward. You could refactor the HSCoreDataStack protocol and just incorporate it into one baseclass, but I kept it as such so that my various implementations were completely separate from one another. So there is a lot of code repeated across implementations. It is a Sandbox project after all and doesn’t represent an incredible approach to architecture.