• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Manual Core Data schema migration in -[NSPersistentDocument configurePersistentStoreCoordinatorForURL:ofType:...] without "document changed" error?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: sanchezm <email@hidden>
  • Date: Wed, 7 Jan 2009 13:46:56 -0800

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

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:
email@hidden


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


  • Follow-Ups:
    • Re: Manual Core Data schema migration in -[NSPersistentDocument configurePersistentStoreCoordinatorForURL:ofType:...] without "document changed" error?
      • From: "Barry Wark" <email@hidden>
  • Prev by Date: How to obtain icon displayed by Finder for a file
  • Next by Date: Re: How to obtain icon displayed by Finder for a file
  • Previous by thread: Re: How to obtain icon displayed by Finder for a file
  • Next by thread: Re: Manual Core Data schema migration in -[NSPersistentDocument configurePersistentStoreCoordinatorForURL:ofType:...] without "document changed" error?
  • Index(es):
    • Date
    • Thread