initialize a store with default data core data
initialize a store with default data core data
- Subject: initialize a store with default data core data
- From: Gustavo Pizano <email@hidden>
- Date: Thu, 21 Jan 2010 14:38:39 +0100
Hello.
I was reading the Apple docs, and in the FAQ it says that I must check for :
f you are using a non-document-based application and started with the standard application template then after these lines of code:
if ( ![fileManager fileExistsAtPath:applicationSupportFolder isDirectory:NULL] )
{
[fileManager createDirectoryAtPath:applicationSupportFolder attributes:nil];
}
url = [NSURL fileURLWithPath: [applicationSupportFolder stringByAppendingPathComponent: @"Delete.xml"]];
you can add a check to determine whether the file at the url exists. If it doesn't, you need to import the data.
now I have the following:
if ( ![fileManager fileExistsAtPath:applicationSupportDirectory isDirectory:NULL] ) {
if (![fileManager createDirectoryAtPath:applicationSupportDirectory withIntermediateDirectories:NO attributes:nil error:&error]) {
NSAssert(NO, ([NSString stringWithFormat:@"Failed to create App Support directory %@ : %@", applicationSupportDirectory,error]));
NSLog(@"Error creating application support directory at %@ : %@",applicationSupportDirectory,error);
return nil;
}
}
NSURL *url = [NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent: @"iZivnostData"]];
//The iZivnostData doesn't exists, so Im gonna import the status
if(url){
NSString * pListPath = [[NSBundle mainBundle] pathForResource:@"Status" ofType:@"plist"];
NSDictionary * statusDic = [[NSDictionary alloc] initWithContentsOfFile:pListPath];
NSEnumerator * enume = [statusDic keyEnumerator];
id key;
while(key = [enume nextObject]){ //Iterate the enumerator to set teh InvoiceStatus
//Status * stat = [NSEntityDescription entityForName:@"Status" inManagedObjectContext:mom];
//NSDictionary * internalDic = [statusDic valueForKey:(NSString*)key];
}
[statusDic release]; //REleasing the Dictionary
}
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: mom];
//Handling autoversioning
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType
configuration:nil
URL:url
options:options
error:&error]){
[[NSApplication sharedApplication] presentError:error];
[persistentStoreCoordinator release], persistentStoreCoordinator = nil;
return nil;
}
return persistentStoreCoordinator;
What is in bold is where I read the plist file and if I understand fine I can import the new data.. BUT ... as you can see I commented the internal while loop, because I need to have a NSManagedObjectContext, which at this point I don't have.. .. I dunno what I did'nt get form the docs..
what am I missing to do.. ? If I call the [self managedObjectContext] method, this method will as for the persistentStorCoordinator which at this point it doesn't exist yet... what to do then?
Best regards
thanks a lot.
Gustavo Pizano_______________________________________________
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