UIManagedDocument can only read documents that are file packages

My application uses a basic SQLite database. I would like my users to use iCloud to synchronize between devices - and I thought I could use UIManagedDocument.

I have subclassed it following Apple documentation and works when you need to create a new persistent file. However, when I try to use it to open the old old repository file, I get the following exception:

"UIManagedDocument can only read documents that are file packages."

Does this mean that I need to move the old permanent store to a new store managed by UIManagedDocument? If this is the case, do I need to do this manually (i.e.. Every time, write every record from the old repository and write it to a new one)?

Thanks in advance!

+4
source share
2 answers

UIManagedDocument creates packages (folders), not atomic ones. The store is still there, but he was buried in a package. If you right-click the file created in the Documents folder in the simulator, you can see the structure. Defaults to

mydocument.foo -> StoreContent -> persistentStore 

What you need to do is create a new extension for your application file type, for example, if the .myappdb database .myappdb you need to create a new document type in the project settings, which can be .myappdbw . You can copy all the settings from the entry for .myappdb

Further, at the moment when you are processing the opening of an obsolete document in mydocumenturl instead of passing it to the permanent repository coordinator, you create the directory structure above.

 NSURL *newurl = [[mydocumenturl URLByDeletingPathExtension] URLByAppendingPathExtension:@"myappdbw"]; NSURL *desturl = [newurl URLByAppendingPathComponent:@"StoreContent"]; [[NSFileManager defaultManager] createDirectoryAtURL:desturl withIntermediateDirectories:YES attributes:nil error:NULL]; NSURL *finalurl = [desturl URLByAppendingPathComponent:@"persistentStore"]; 

and then move the obsolete database to the folder you created.

 [[NSFileManager defaultManager] moveItemAtURL:mydocumenturl toURL:finalurl error:NULL]; 

and then you can pass the url of the UIManagedDocument package

 UIManagedDocument *doc = [[UIManagedDocument alloc] initWithFileURL:newurl]; 

A link that will be useful for iCloud integration,

http://developer.apple.com/library/ios/#releasenotes/DataManagement/RN-iCloudCoreData/_index.html

This is all a bit mysterious, since most of the promised code sample has not yet appeared, but, on the other hand, it is basically quite simple to output. Take a look at WWDC2011 sessions 107,116 and 315 for more tips.

But note that if you intend to use this method to migrate legacy DONT documents, set NSPersistentStoreUbiquitousContentNameKey to the point that you migrate because the package changes when you do. The dock above describes this pretty well.

+5
source

Thanks for this tip. I think I have found an even simpler solution.

I just create a new UIManagedDocument with a different file name than my old persistent storage.

In my subclass of UIManagedDocument I override the configurePersistentStoreCoordinatorForURL method and do the migration once there:

 - (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)storeURL ofType:(NSString *)fileType modelConfiguration:(NSString *)configuration storeOptions:(NSDictionary *)storeOptions error:(NSError **)error { // If legacy store exists, copy it to the new location NSFileManager* fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:legacyPersistentStoreURL.path]) { NSError* thisError = nil; [fileManager copyItemAtURL:legacyPersistentStoreURL toURL:storeURL error:&thisError]; [fileManager removeItemAtURL:legacyPersistentStoreURL error:&thisError]; } return [super configurePersistentStoreCoordinatorForURL:storeURL ofType:fileType modelConfiguration:configuration storeOptions:storeOptions error:error]; } 
+1
source

Source: https://habr.com/ru/post/1385540/


All Articles