Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
Hopefully you guys (and girls) have some good suggestions on this problem. Thanks in advance.

I create an file package, in which I want to save an coredata file. If I treat the coredata file as an ordinary text file, and have my own class (which uses the core data functions) to deal with it, it should not matter that it is in file package or not. My project is document based, not document with coredata.

The method below is the write method of my class.
What works is, that I can create a file package and add the coredata file to it. I see it when I inspect the package contents in finder.
What doesn't work is saving a second time. Neither save the original package, nor saving in a new location (saveAs).

I am guessing that this has to something to do with temporary directories.
First time I save, I go from inmemorystore to an xmlmemorystore. But that file is first saved in a temporary file, and then moved to its correct location.

The second time I save, that location doesn't exist anymore. Moving the store to a new location gives the same error. Perhaps it's not so much that it can't save in the new location, but that it can't find the original file on disk?

Any ideas how to correct for this?

Code:
-(BOOL) saveToFile:(NSURL *) filebundle {
	//do not assume filebundle url is same as original file. Could be temporary directories.
	//thus assume file location is always a new place
	//see NSDocument Class Reference
	NSError *error = nil;
	//determine filepath
	NSString *file = [[[[filebundle path] lastPathComponent] stringByDeletingPathExtension] stringByAppendingPathExtension:defstoreextension];
	NSURL *storeurl = [NSURL fileURLWithPath:[[filebundle path] stringByAppendingPathComponent:file]];

	//determine store
	if ([store type] == NSInMemoryStoreType){
		//new store
		store = [psc migratePersistentStore:store //old store is removed from psc
									  toURL:storeurl
									options:nil
								   withType:defstoretype
									  error:&error];
		if (error) {
			NSLog(@"could not migrate store from memory to disk");
			return NO; //fail
		}
	} else {
		//existing store
		if (![psc setURL:storeurl forPersistentStore:store]) { //move store to new location   // ---> second pass <--- I also tried migratePersistentStore
			NSLog(@"could not migrate store to new location: %@",[storeurl path]);
			return NO; //fail
		}
	}
	
	//save store
	if (![moc save:&error]){ //save store
		NSLog(@"Could not save store: %@",[error localizedDescription] );   // ---> error on second pass <--- 
		return NO; //fail
	}
	return YES; //succes
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.