9

My iCloud Core Data app was running great on iOS7 and ready to launch. When I test on iOS 8 I get the following error and can't seem to fix it when trying to upload data to iCloud.

I suspect my problem is related to how I am getting the document directory and changes in the doc directory with iOS8 but I just can't figure this out..

014-10-12 15:14:17.862 XXXXXXX [4662:236693] __45-[PFUbiquityFilePresenter processPendingURLs]_block_invoke(439): CoreData: Ubiquity:  Librarian returned a serious error for starting downloads Error Domain=BRCloudDocsErrorDomain Code=6 "The operation couldn’t be completed. (BRCloudDocsErrorDomain error 6 - Path is outside of any CloudDocs container, will never sync)" UserInfo=0x7f8b1a525f60 {NSDescription=Path is outside of any CloudDocs container, will never sync, NSFilePath=/Users/garyrea/Library/Developer/CoreSimulator/Devices/9AADFE8E-5ECC-4969-9418-57DA45B747C9/data/Containers/Data/Application/AD2E5E62-7295-4371-A08D-1790E8FCCD96/Documents/CoreDataUbiquitySupport/nobody~simA28745A4-A67F-598C-9260-F9AC36609ECF/iCloud/5B8BFA36-1ACA-4966-B7ED-A7344D36ACF1/container/nobody~simA28745A4-A67F-598C-9260-F9AC36609ECF/iCloud/2trlqdMQVpJ~wlEfiLvjWtQfrUJ8YiNCd84KW_xiw4A=/F0CF5F29-D437-4728-B0A2-C5BB90BBC239.1.cdt} with userInfo {
    NSDescription = "Path is outside of any CloudDocs container, will never sync";
    NSFilePath = "/Users/garyrea/Library/Developer/CoreSimulator/Devices/9AADFE8E-5ECC-4969-9418-57DA45B747C9/data/Containers/Data/Application/AD2E5E62-7295-4371-A08D-1790E8FCCD96/Documents/CoreDataUbiquitySupport/nobody~simA28745A4-A67F-598C-9260-F9AC36609ECF/iCloud/5B8BFA36-1ACA-4966-B7ED-A7344D36ACF1/container/nobody~simA28745A4-A67F-598C-9260-F9AC36609ECF/iCloud/2trlqdMQVpJ~wlEfiLvjWtQfrUJ8YiNCd84KW_xiw4A=/F0CF5F29-D437-4728-B0A2-C5BB90BBC239.1.cdt";
} for these urls: (
    "file:///Users/garyrea/Library/Developer/CoreSimulator/Devices/9AADFE8E-5ECC-4969-9418-57DA45B747C9/data/Containers/Data/Application/AD2E5E62-7295-4371-A08D-1790E8FCCD96/Documents/CoreDataUbiquitySupport/nobody~simA28745A4-A67F-598C-9260-F9AC36609ECF/iCloud/5B8BFA36-1ACA-4966-B7ED-A7344D36ACF1/container/nobody~simA28745A4-A67F-598C-9260-F9AC36609ECF/iCloud/2trlqdMQVpJ~wlEfiLvjWtQfrUJ8YiNCd84KW_xiw4A=/F0CF5F29-D437-4728-B0A2-C5BB90BBC239.1.cdt"
)

my app delegate extension code where I create my persistent store is as follows. I have a seed database for first time installation.

- (NSPersistentStoreCoordinator *)createPersistentStoreCoordinator{
   NSPersistentStoreCoordinator *persistentStoreCoordinator = nil;
   NSManagedObjectModel *managedObjectModel = [self createManagedObjectModel];
   persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:managedObjectModel];
   NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@
   "CoreData.sqlite"];    

   if (![[NSFileManager defaultManager]fileExistsAtPath:[storeURL path]]){
       NSURL *preloadURL=[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"SeedDatabase" ofType:@
       "sqlite"]];
       NSError *error=nil;
       if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&error]){
           NSLog(@
           "File couldnt save");
       }
   }


   NSUbiquitousKeyValueStore *kvStore=[NSUbiquitousKeyValueStore defaultStore];
   if (![kvStore boolForKey:@"SEEDED_DATA"]){
       NSLog (@
       "In the new database");
       NSURL *seedStoreURL=[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"SeedDatabase" ofType:@
       "sqlite"]];
       NSError *seedStoreErrpr;
       NSDictionary *seedStoreOptions=@{NSReadOnlyPersistentStoreOption: @YES};
       NSPersistentStore *seedStore=[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:seedStoreURL options:seedStoreOptions error:&seedStoreErrpr];

       NSDictionary *iCloudOptions =@{NSPersistentStoreUbiquitousContentNameKey: @"iCloud",
                                       NSMigratePersistentStoresAutomaticallyOption:@YES,
                                       NSInferMappingModelAutomaticallyOption:@YES
       };

       NSOperationQueue *queue=[[NSOperationQueue alloc] init];
       [queue addOperationWithBlock:^{
           NSError *error;
           [persistentStoreCoordinator migratePersistentStore:seedStore toURL:storeURL options:iCloudOptions withType:NSSQLiteStoreType error:&error];
           NSLog(@
           "Persistant store migrated");
           [kvStore setBool:YES forKey:@
           "SEEDED_DATA"];
          // [self checkForDuplicates];
       }];
   }else{
       NSError *error;
       NSDictionary *storeOptions =@{NSPersistentStoreUbiquitousContentNameKey: @
          "iCloud"
       };
       if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                        configuration:nil
                                        URL:storeURL
                                        options:storeOptions
                                        error:&error]) {
           NSLog(@
           "Unresolved error %@, %@", error, [error userInfo]);
           abort();
       }
   }

   return persistentStoreCoordinator;
}

- (NSURL *)applicationDocumentsDirectory{
   return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory    inDomains:NSUserDomainMask] lastObject];
}
Shmidt
  • 16,436
  • 18
  • 88
  • 136
grayentropy
  • 151
  • 1
  • 5
  • 1
    Did up end up with a solution? same error here.. – MatterGoal Oct 16 '14 at 08:07
  • 1
    I was able to resolve this by specifying the iCloud directory in the icloudOptions dictionary. I also needed to completely remove it from all devices, delete the iCloud drive files and restart on a device. Post updated – grayentropy Oct 18 '14 at 14:09
  • I am getting the same error and wondering what do you do if the user has iCloud switched off and the `NSFileManager` returns `nil` when you ask for the cloud directory? – Rob Sanders Dec 12 '14 at 13:15
  • @grayentropy: You’re lucky that it finally worked for you. None of the described steps solve the same issue on my setup (admittedly with OS X 10.11 El Capitan & iOS 9 betas). Even when I completely remove everything (local storage from all peers & cloud storage) I get the same error messages and changes never propagate from iOS to OS X. – Rafael Bugajewski Aug 24 '15 at 09:43

2 Answers2

5

I was able to resolve this error by specifying the iCloud drive directory (Same name as the one on the developer.apple.com interface).

-(NSURL *)cloudDirectory
{
NSFileManager *fileManager=[NSFileManager defaultManager];
NSString *teamID=@"iCloud";
NSString *bundleID=[[NSBundle mainBundle]bundleIdentifier];
NSString *cloudRoot=[NSString stringWithFormat:@"%@.%@",teamID,bundleID];
NSURL *cloudRootURL=[fileManager URLForUbiquityContainerIdentifier:cloudRoot];
NSLog (@"cloudRootURL=%@",cloudRootURL);
return cloudRootURL;
}

and including it in the icloudOptions Dictionary as a NSPersistentStoreUbiquitousContentURLKey

NSDictionary *storeOptions =@{NSPersistentStoreUbiquitousContentNameKey: @"iCloud",
                                   NSPersistentStoreUbiquitousContentURLKey:[self cloudDirectory],
                                   };

I was getting some strange errors so I removed the app from all devices, deleted the iCloud drive file and re ran on an actual device and it worked fine. Not sure if it runs on IOS7 now but since I only specified the NSPersistentStoreUbiquitousContentURLKey I am pretty confident it should be fine.

grayentropy
  • 151
  • 1
  • 5
  • 1
    I tried this but I get: "Unable to continue parse components of URL: /var/mobile/Library/Mobile Documents/iCloud~com~example~foo/CoreData/MyAppCloudStore/.baseline/MyAppCloudStore" – User May 29 '15 at 10:46
  • What did you set at the teamID? – user3065901 Jul 15 '15 at 11:30
-1

I had the same issue during loading some test data. For the load of the data I was deleting all records. To avoid the exception a simple sleep(1) between cleaning and loading was enough.

DerMarcus
  • 189
  • 1
  • 9