Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [78]

By Root 981 0

The persistentStoreCoordinator Accessor Method


Next up is the considerably longer persistentStoreCoordinator method:

This method starts by looking for an existing object in the persistentStoreCoordinator instance variable, returning it if there is one. Otherwise, it needs to initialize the coordinator itself, so it goes on to look for a managedObjectModel by calling the method of the same name. If it doesn’t exist, then the application contains no model, and this method logs an error and returns nil. Otherwise, it goes on to check for the existence of a directory (specified by the value returned by the applicationSupportDirectory method) where the app’s Core Data storage should be loaded. If the directory doesn’t exist, it tries to create it; and if that doesn’t work, it logs an error and returns nil.

If we get past all those edge cases (and we usually will), then the method builds a URL pointing at where the storage file should be located, creates an NSPersistentStoreCoordinator, and tells the coordinator to add a new store, given the URL. The coordinator is smart enough to look at the URL, figure out if there’s already a persistent store there, and if not, create one for us. If it encounters any error along the way, it tells us so, and the application reports an error to the user.

A few points of interest about this method: for one thing, this is where the actual filename for your app’s data store (currently storedata, though under Leopard it defaulted to MythBase.xml) is specified. If you wanted to name your file something else, this is where you’d change it. Also, this is where the type of the backing store is specified. It’s currently set to NSXMLStoreType, which means that the stored data will be in an XML-formatted file. This sort of storage is nice while you’re developing your app, since the resulting file is easy to parse with other tools, or for that matter read with your own eyes. However, before delivering a final application, you may want to consider changing your data store to another type, for the sake of size, speed, and other considerations. The other choices you have are NSBinaryStoreType, which saves its data in a binary format that takes less disk space and is faster to read and write, and NSSQLiteStoreType, which in addition to the advantages of NSBinaryStoreType also frees the persistent store from the burden of holding the entire object graph in memory at once; it only loads objects as they’re accessed. That difference becomes crucial when dealing with a large data set. Because of this advantage, we recommend changing from NSXMLStoreType to NSSQLiteStoreType before releasing your application. There’s an additional store type, NSInMemoryStoreType, which can be used for maintaining an in-memory object graph that’s never saved to disk. The default Core Data application template does a pretty good job of tucking away the data storage for you so that you don’t have to give it too much thought, but at some point before shipping your app you will have to decide which of these formats to use for your app’s storage. We’ve given our recommendations, but you might want to consult the documentation in Xcode (search for “core data programming guide” to gain more understanding before deciding how to tackle this issue.

The final thing to point out about persistentStoreCoordinator is that if you want to partition your data into different stores (for example one that’s on disk and one that’s only in memory, disappearing when the app terminates) this is where you’d need to make the changes, adding each of your persistent stores to the coordinator.

The managedObjectContext Accessor Method


Now let’s look at the managedObjectContext method, which is the getter for the read-only property of the same name:

Like the other methods we’ve seen acting as a getter for a property, this method first checks to see if the instance variable has been set, and if so, returns it. Otherwise, it checks for the existence of a persistent store coordinator by calling the persistentStoreCoordinator method. If that

Return Main Page Previous Page Next Page

®Online Book Reader