small medium large xlarge

Back to: All Forums  Core Data
Generic-user-small
08 Apr 2009, 21:50
Felipe Laso (104 posts)

Hello.

I currently have an application that lists food ingredients and the amount of proteins each one has. I used a .plist file to load the data whenever the application is launched.

The thing is I now want to write a new version that allows the user to add or remove their own food ingredients.

BUT, I have not found any articles in the documentation or on the internet that suggest how to pre populate a Core Data application. Now I know there is SQLite, but Core Data seems much simpler and easier to implement in my case.

Basically in SQLite I could just add the existing database file with tables and data into it, then when the application is loaded and run for the first time it will contain the data in my SQLite database.

Is there a way to do this in Core Data? Either via commands or through a graphical user interface?

If there is no easy way to do this, and I have to programmatically create my NSDictionary with the food ingredients, then store it with Core Data the first time my app is launched, then I’m afraid I might have to revert to using SQLite for this (which im trying to avoid since it seems rather complicated and I don’t know much SQLite).

Thanks in advance, Felipe.

Avatarsmall_pragsmall
10 Apr 2009, 15:42
Marcus S. Zarra (284 posts)

This is discussed in the book but I don’t think the chapter in question is available yet. However it is fairly simple. You have several options and the easiest is to store a copy of the database in your application with the data pre-populated. When the user wants to create a new document you copy that database over instead of creating a new one.

Generic-user-small
20 Apr 2009, 00:07
Jericho Hasselbush (3 posts)

Hi Felipe, I’ve been working on an application that uses Core Data and in order to pre-populate a store, I create the default store by running the application, and then putting in the data. I then move that file from it’s saved location, and put it into my Xcode project, save it as a resource and import it when the application is run for the first time by checking if there is already a saved file. Here is the sample code that may help you. It is placed in the appDelegate’s - (NSPersistentStoreCoordinator *) persistentStoreCoordinator method.

// import default information from DefaultData.xml
	NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:[applicationSupportFolder stringByAppendingPathComponent:@"DefaultData.xml"]];
	if (fileHandle == nil) {
		NSPersistentStoreCoordinator *defaultDataStoreCoordinator;
		NSString *defaultPath = [[NSBundle mainBundle] pathForResource:@"DefaultData" ofType:@"xml"];
		if (defaultPath) {
			NSURL *defaultURL = [NSURL fileURLWithPath:defaultPath];
			defaultDataStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
			if(![defaultDataStoreCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:defaultURL options:nil error:&error]) {
				[[NSApplication sharedApplication] presentError:error];
			}
			else {
				if (![defaultDataStoreCoordinator migratePersistentStore:[defaultDataStoreCoordinator persistentStoreForURL:defaultURL]
																   toURL:url options:nil withType:NSXMLStoreType error:&error]) {
					[[NSApplication sharedApplication] presentError:error];
				}
			}
		}
		else {
			// create a store and file from scratch
			persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
			if (![persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]){
				[[NSApplication sharedApplication] presentError:error];
			}
		}
	}
	// load data from stored user support folder
	else {
		persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
		if (![persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]){
			[[NSApplication sharedApplication] presentError:error];
		}    
	}
Generic-user-small
16 Jun 2009, 08:40
Bala Paranj (20 posts)

I followed the blog post of Marcus that shows you how to import data in a CSV file into a Core Data application. Now I want to export this data into a new project where I need it as seed data. How can I do this? I just purchased the ebook, I have not received the link to the download yet. TIA.

Avatarsmall_pragsmall
27 Jul 2009, 05:55
Marcus S. Zarra (284 posts)

There is nothing to export if you want to keep it in a Core Data repository. Just grab the saved repository file and include it as a resource in the new project. I believe this same answer was given to you on the MDN blog post.

Generic-user-small
30 Oct 2010, 17:15
Matt Di Pasquale (1 post)

I Googled and found out from this post that CoreDataBooks does this. Scroll down to the persistantManagedStore function. It’s the best implementation I’ve seen. Basically, it checks to see if the SQL file already exists. If not, it copies a default one from the Resources directory there. This default file can be pre-populated however you’d like.

You must be logged in to comment