0

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;
    }
}
Matt Zera
  • 465
  • 3
  • 12
  • You should probably be creating the document in the local store. Core Data will handle the iCloud setup for you. Take a look at the WWDC 2013 session 207 – Duncan Groenewald Oct 28 '13 at 11:15
  • Have you checked whether the original UIManagedDocument is getting deleted from the device when you remove the App and whether it is being correctly replaced when you reinstall the App ? I follow the 207 recommendation of creating the UIManagedDocument in the local app sandbox and that works fine for me. If you want I can post the document creation code, its quite similar to what you have except I leave all the iCloud stuff to Core Data. I gave up on the other approach completely, mainly because it does not seem to work with OS X. – Duncan Groenewald Oct 29 '13 at 23:54

0 Answers0