Graphics Programs Reference
In-Depth Information
Core Data and iCloud
So far, you know that iCloud keeps files in a ubiquity container in the cloud and that iOS
devices with access to that ubiquity container pull down copies of that file and push back
any edits. However, Core Data does something a little different.
Core Data is an interface to a single SQLite library file, so you might think we would store
this file in the cloud. But this is a bad idea because a SQLite file can be very large and
changing the data inside the file would require uploading the whole file to the cloud and
downloading the whole thing to each device.
Instead, the SQLite file that Core Data talks to remains on the device's local filesystem,
and Core Data stores a transaction log file in the cloud. When a change is made with Core
Data, the change occurs in the local SQLite file and is noted in the transaction log file. Oth-
er devices pull the transaction log file down and use the information in it to modify their
own SQLite file.
When setting up a Core Data persistent store, you can specify that it read from a transaction
log stored in a ubiquity container. In BNRFeedStore.m , add the following code to
init .
- (id)init
{
self = [super init];
if (self) {
model = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator *psc =
[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// Find the location of the ubiquity container on the local filesystem
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *ubContainer = [fm URLForUbiquityContainerIdentifier:nil];
// Construct the dictionary that tells Core Data where the
// transaction log should be stored
NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setObject:@"nerdfeed"
forKey:NSPersistentStoreUbiquitousContentNameKey];
[options setObject:ubContainer
forKey:NSPersistentStoreUbiquitousContentURLKey];
NSError *error = nil;
NSString *dbPath =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
Search WWH ::




Custom Search