Database Reference
In-Depth Information
To build a new Core Data desktop application and implement iCloud data
syncing, refer to Section 6.2, Direct NSManagedObjectContext to iCloud , on
page 106 for information, because the steps are identical. Further, to migrate
an existing desktop application that uses a traditional Core Data stack, refer
to Section 6.5, Migrating an Existing Application , on page 113 , because that is
also identical.
If we are using an NSPersistentDocument , things get a little interesting—not a lot
but enough to merit attention. The first thing we need to do is to subclass
NSPersistentDocument . The reason for this is that unlike with UIManagedDocument ,
there is no way to pass in options to the NSPersistentStore when it is being added
to the NSPersistentStoreCoordinator . That's the reason for subclassing.
DepartmentAndEmployees/MyDocument.m
- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL*)url
ofType:(NSString*)fileType
modelConfiguration:(NSString*)configuration
storeOptions:(NSDictionary*)storeOptions
error:(NSError**)error
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];
if (cloudURL) {
NSString *pathComponent = [url lastPathComponent];
cloudURL = [cloudURL URLByAppendingPathComponent:pathComponent];
NSMutableDictionary *mutableOptions = [storeOptions mutableCopy];
[mutableOptions setValue:[[NSBundle mainBundle] bundleIdentifier]
forKey:NSPersistentStoreUbiquitousContentNameKey];
[mutableOptions setValue:cloudURL
forKey:NSPersistentStoreUbiquitousContentURLKey];
storeOptions = mutableOptions;
}
return [super configurePersistentStoreCoordinatorForURL:url
ofType:fileType
modelConfiguration:configuration
storeOptions:storeOptions
error:error];
}
In this override, we first check to see whether iCloud is enabled. If it is, we
build our full iCloud URL and then add the two options required to link our
persistent store with iCloud. Once the options dictionary has been updated,
we return control to our super's implementation.
 
 
 
Search WWH ::




Custom Search