//
you're reading...

Programming

Introducing the Core Data Controller

Cloud SyncAs I was developing Speech Timer 2, initially I used a document class to manage my Core Data stack – that is, UIManagedDocument on iOS and BSManagedDocument on OS X. Those two gets derived into one subclass and a preprocessor statement selects the right superclass at compile time. The reason for using those were primarily convenience: they both manage a Core Data stack and have similar APIs so that I can use one class to inherit from both of them with only minimal variations on the two platforms. Another reason was window management on the Mac – NSDocument has some fancy way to manage windows and NSWindowController objects already has a reference to its document – so that I don’t need to manage yet another reference to the core data stack, namely NSManagedObjectContext instance.

Things were nice until I tried adding in iCloud support. As of this writing, BSManagedDocument isn’t yet tested to incrementally sync a Core Data based document over iCloud. I’ve had some success in adding support for iCloud there but then hit a brick wall: first save of a document often fails with a "The file couldn’t be saved because you don’t have permission.” error. I was baffled.

This iCloud thing have been delaying the project for quite some time and I’ll need to fix it rather quick. Since I’m essentially re-implementing UIManagedDocument on the Mac, there may be some other things that the original class does that I need to emulate to get the two to sync. Stuff that I haven’t got much time to do.

Then I realized: Speech Timer is a library-style app. There is only one document, ever and the user doesn’t need to be presented with open/save dialogs and all the standard things that a document-based app do. The whole convenience of NSDocument s window management on the Mac is negated by the additional efforts I need to make. So I decided to rip out that document-based implementation and take the best bits of BSManagedDocument and bring it to both iOS and OS X.

Introducing BSCoreDataController: your Core Data stack manager for iOS and OS X. It’s loosely inspired by UIManagedDocument but pared down to its bare necessities for supporting a library-style app. Additionally there are a number of convenience functions to make iCloud easier to use by a Core Data app.

Like UIManagedDocument & BSManagedDocument classes that came before it, the class features:

  • Dual nested managed object contexts for maximum responsiveness of your application due to non-blocking I/O.
  • Saves the data inside a file package, which is important in 10.9 and iOS 7 since by default the Core Data store file now consists of more than one file due to SQLite write-ahead logging mode that was made default in those OS releases.
  • Support for iCloud’s persistent store changes.
  • Support for Autosave.

However it doesn’t have the document baggages that a library-style app doesn’t use:

  • No support for Save As, Duplicate, Save To, and the things that you’ll need to do if you expose document management functionality to the user.
  • Designed to be served from your Application Support folder and not from your Documents directory.

So I’ve posted its preliminary version on github: adib/BSCoreDataController – freshly forked out of Speech Timer 2’s main project. Enjoy!

 

 



Do you enjoy this post? Enter your e-mail address below to receive articles like this one in your mailbox.

Free Updates!

Learn how to grow your indie business while keeping your day job.

Categories

Archives

Keep updated!

Don't miss out on new articles!