Re: Manual Core Data schema migration in -[NSPersistentDocument configurePersistentStoreCoordinatorForURL:ofType:...] without "document changed" error?
Re: Manual Core Data schema migration in -[NSPersistentDocument configurePersistentStoreCoordinatorForURL:ofType:...] without "document changed" error?
- Subject: Re: Manual Core Data schema migration in -[NSPersistentDocument configurePersistentStoreCoordinatorForURL:ofType:...] without "document changed" error?
- From: "Barry Wark" <email@hidden>
- Date: Wed, 7 Jan 2009 14:45:30 -0800
On Wed, Jan 7, 2009 at 1:46 PM, sanchezm <email@hidden> wrote:
> NSDocument tracks the moving of the file and this is done after the code in
> configurePersistentStore... is executed.
> One way to possibly work around this is to delay the setting of the URL and
> modification date. At the end of your configurePersistentStore...
> implementation, do a call to
>
>
> [self performSelector:@selector(setDelayedURL:) withObject:url
> afterDelay:0.5f];
>
> and then
>
> - (void)setDelayedURL:(NSURL *)url {
> [self setFileURL:url];
> [self setFileModificationDate:[[[NSFileManager defaultManager]
> fileAttributesAtPath:[url path] traverseLink:YES] fileModificationDate]];
>
> }
>
> - Miguel
Thanks for the info. I had been working on the assumption that the
tracking was set up before configurePersistentStore... For those that
are curious, an other working solution is documented here:
http://stackoverflow.com/questions/380076/manual-core-data-schema-migration-without-document-changed-warning
.
>
> On Dec 15, 2008, at 10:29 AM, Barry Wark wrote:
>
>> The data model for my Core Data document-based app (10.5 only) is in a
>> framework, so automatic schema upgrades using a Core Data mapping
>> model don't appear to work. It appears that the Core Data machinery
>> doesn't find the appropriate data models or mapping model when they
>> are not in the app's main bundle. So, instead of using the automatic
>> migration, I'm running a migration manually in
>> configurePersistentStoreCoordinatorForURL:ofType:... in my
>> NSPersistenDocument subclass (code below). I migrate the persistent
>> store to a temporary file and then overwrite the existing file if the
>> migration succeeds. The document then presents an error with the
>> message "This document's file has been changed by another application
>> since you opened or saved it." when I try to save. As others on this
>> list have pointed out, this is due to my modification of the
>> document's file "behind its back". I tried updating the document's
>> file modification date, as shown below, but I then get an error dialog
>> with the message "The location of the document "test.ovproj" cannot be
>> determined." when I try to save. I'm less sure of the reason for this
>> error, but trading one unnecessary message (in this case) for an other
>> isn't quite what I was going for.
>>
>> Can anyone offer some guidance? Is there a way to manually upgrade the
>> schema for a document's persistent store without triggering one of
>> these (in _this_ case unnecessary) warnings?
>>
>> thanks,
>> Barry
>>
>>
>> code for upgrading the data store in my subclasses
>> -configurePersistentStoreCoordinatorForURL:ofType:... :
>>
>> if(upgradeNeeded) {
>> NSManagedObjectModel *sourceModel = [NSManagedObjectModel
>> mergedModelFromBundles:VUIModelBundles()
>>
>> forStoreMetadata:meta];
>>
>> if(sourceModel == nil) {
>> *error = [NSError errorWithDomain:VUIErrorDomain
>> code:VUICoreDataErrorCode localizedReason:BWLocalizedString(@"Unable
>> to find original data model for project.")];
>> return NO;
>> }
>>
>> NSManagedObjectModel *destinationModel = [self
>> managedObjectModel];
>>
>> NSMigrationManager *migrationManager =
>> [[NSMigrationManager alloc] initWithSourceModel:sourceModel
>> destinationModel:destinationModel];
>> NSMappingModel *mappingModel = [NSMappingModel
>> mappingModelFromBundles:VUIModelBundles()
>>
>> forSourceModel:sourceModel
>>
>> destinationModel:destinationModel];
>> if(mappingModel == nil) {
>> *error = [NSError errorWithDomain:VUIErrorDomain
>> code:VUICoreDataErrorCode localizedReason:BWLocalizedString(@"Unable
>> to find mapping model to convert project to most recent project
>> format.")];
>> return NO;
>> }
>>
>> @try {
>> //move file to backup
>> NSAssert([url isFileURL], @"store url is not a file URL");
>>
>> NSString *tmpPath = [NSString tempFilePath];
>> id storeType = [meta objectForKey:NSStoreTypeKey];
>> if(![migrationManager migrateStoreFromURL:url
>> type:storeType
>> options:storeOptions
>> withMappingModel:mappingModel
>> toDestinationURL:[NSURL
>> fileURLWithPath:tmpPath]
>> destinationType:storeType
>> destinationOptions:storeOptions
>> error:error]) {
>>
>> return NO;
>> } else {
>> //replace old with new
>> if(![[NSFileManager defaultManager]
>> removeItemAtPath:[url path] error:error] ||
>> ![[NSFileManager defaultManager]
>> moveItemAtPath:tmpPath toPath:[url path] error:error]) {
>> return NO;
>> }
>>
>> // update document file modification date to
>> prevent warning (#292)
>> NSDate *newModificationDate = [[[NSFileManager
>> defaultManager] fileAttributesAtPath:[url path] traverseLink:NO]
>> objectForKey:NSFileModificationDate];
>> [self setFileModificationDate:newModificationDate];
>> }
>> }
>> @finally {
>> [migrationManager release];
>> }
>> }
>> }
>>
>> return [super configurePersistentStoreCoordinatorForURL:url
>> ofType:fileType modelConfiguration:configuration
>> storeOptions:storeOptions error:error];
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden