I have an app that I'm making and it uses a UIManagedDocument
to handle core data and iCloud. At the start of my app I Open the file and initialize the UIManagedDocument
with the iCloud directory.
This works fine the first time I load the program and subsequent times but It is my understanding that because I use iCloud I should be able to delete the app and reload it and have it reload the data from the cloud.
When I delete the app and reload it its able to get the url and initialize the UIManagedDocument
but once it gets to [document openWithCompletionHandler:^(BOOL success){}]
the thread that was supposed to access the document comes back with EXC_BAD_ACCESS.
This is the code I use to setup the document with core data and the iCloud url is good. This is my first experience with iCloud. What am I doing wrong?
- (void)setupDocument
{
NSURL *url = [self iCloudDocumentsDirectory];
url = [url URLByAppendingPathComponent:@"ZJournalData"];
UIManagedDocument *document = [[ZManagedDocument alloc] initWithFileURL:url];
NSLog(@"%@", document);
[document.persistentStoreOptions setValue:[document.fileURL lastPathComponent] forKey:NSPersistentStoreUbiquitousContentNameKey];
[document.persistentStoreOptions setValue:[[self iCloudDirectory] URLByAppendingPathComponent:@"CoreDataLogs"] forKey:NSPersistentStoreUbiquitousContentURLKey];
if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
[document saveToURL:url
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success) {
if (success) {
self.managedObjectContext = document.managedObjectContext;
}
}];
} else if (document.documentState == UIDocumentStateClosed) {
[document openWithCompletionHandler:^(BOOL success) {
if (success) {
self.managedObjectContext = document.managedObjectContext;
}
}];
} else {
NSLog(@"%@", document);
self.managedObjectContext = document.managedObjectContext;
}
}