Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-17-2009, 03:57 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 20
Unhappy Core Data Basics

Hi,

I am trying to save and retrieve a hierarchy of objects. I know its not easy to do it with sqlite3.


Is it possible to do this with Core Data ? can I save all the objects and retrieve them, modify them save them back to file using core data?

Are they are sample code/ tutorial to do this?

Google didn't help me

Thanks for replying.
cation007 is offline   Reply With Quote
Old 07-17-2009, 10:19 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 7
Default

In short: Yes. It's possible, if I understand what you're talking about. Is it easy? No F'ing way. I just got done implementing core data and it was a real bitch. Most of the tutorials and examples floating out there don't do much good in explaining WTF is going on ... they just show you how to get it working, not how to get it working for what YOU want to do with it.

I wound up buying the eBook on the iPhone SDK from Pragmatic Bookshelf. It's got a chapter on Core Data. While it didn't go into much detail on relationships, it was a good start.

I've got a many-to-many relationship between two object models. I wound up creating a third object model to act as an intermediary ... it does one-to-many to my main objects, and thus in the end I've got a many-to-many relationship.

The real bitch for me was that I had to build in the Core Data implementation in stages. First I implemented one object model, and got it populated with a property list. Then, I'd start implementing the middle object model (keeping it ignorant of either of the two main models) ... get it populated with dummy data, just to make sure I had it working. Then I created the relationship from it to the first one, which broke my persistent store without telling me why it was failing. Then once I had that relationship working and populating, I went over to the third model and built it up by itself and populating with it's property list. Then I connected it over to the middle object model ... then magically it all works.

Apple's documentation on Core Data is a f*ing joke. I'm serious. Totally worthless.

The most powerful thing out of all of this is the predicates. But they also are the hardest to wrap your head around. No one has much GOOD documentation on how you should use them to get your crap together.




Quote:
Originally Posted by cation007 View Post
Hi,

I am trying to save and retrieve a hierarchy of objects. I know its not easy to do it with sqlite3.


Is it possible to do this with Core Data ? can I save all the objects and retrieve them, modify them save them back to file using core data?

Are they are sample code/ tutorial to do this?

Google didn't help me

Thanks for replying.
grgcombs is offline   Reply With Quote
Old 07-18-2009, 02:09 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 20
Default

Thanks for replying.

I completely agree with your comments.

I guess I will just use sqlite3. Easy to understand, easy to code.

May be they improve/ make it more easy to use Core Data in future updates.
cation007 is offline   Reply With Quote
Old 07-27-2009, 06:44 AM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 5
Default

You're bang on. I thought it was me being thick struggling to make sense of using core data. All I wanted to learn was how to maintain persistent data. I got caught up in the move to OS 3 and was advised to use core data. Sounded simple until I tried it. I've still not found any spot on resource yet for walking through a meaningful real world app that I could use. If anyone finds something that was a real help please let me know. Until then I'll be trying to make sense of it. Regards.
Somerset is offline   Reply With Quote
Old 07-27-2009, 07:16 AM   #5 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: michigan
Posts: 184
Default

Quote:
Originally Posted by Somerset View Post
You're bang on. I thought it was me being thick struggling to make sense of using core data. .

You need to work thru the Apple tutorial and build the locations project. I started with photolocations as the basis for my application, once I undertood the data model I started changing and adding entities. After that it got to be really easy.

Of course I use sql on the job a lot, and am no stranger to data modeling. The tools built into xcode are probably medium scale as far as modeling tools go but they get the job done.

Once you understand core data you begin to wonder why it was so painful to learn.

Start with the apple tutorials
Then Read the getting started on core data doc
The conference sample in the pragmatic book is good, but some things don't work quite right on it.

The apple samples to look at are

coredatabooks
photoLocations
taggedLocations

And of course locations
michelle is offline   Reply With Quote
Old 07-27-2009, 12:41 PM   #6 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 5
Default

Thanks for the heads up on the samples. I'm getting thru the tutorials and samples, but trying to understand everything at first read got me bogged down. I can now push on with your tips knowing it got easy for you.
Somerset is offline   Reply With Quote
Old 07-27-2009, 01:05 PM   #7 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: michigan
Posts: 184
Default

Quote:
Originally Posted by Somerset View Post
Thanks for the heads up on the samples. I'm getting thru the tutorials and samples, but trying to understand everything at first read got me bogged down. I can now push on with your tips knowing it got easy for you.
No problem, here's a couple more.

Start with the project from the locations tutorial. There are three samples..

locations, photolocations and taggedlocations.

Taggedlocations is a nice one to start on.

Click on the model to bring up the modeler.

now Try adding an entity

Tagged locations has a single location, photolocations has two entities, An event and a photo, there is a 1 to 1 relationship here.


I would suggest adding a catagory entity, again make it 1 to 1, as every event should be tied to a single category for simplicity. I suppose you could make it 1 to many if you want. ie.. multiple categories for each event.


Click the plus sign under entitiy to add the new entity, now add some fields, make sure you define a relationship on the event (ie. category) between the event and the category.

Now rename all the current entity class or save it somewhere, now generate the new model. A new event class and a category class will be generated. Copy any custom stuff thats in your saved entity class to the new class.

Its best when first learining to let the modeler generate the code, later after you get a hang of it, you can add the class attributes yourshelf.

Now very important, assuming you are working in the simulator go to

yourUser/applications/iphoneSimulator/somelongstring/documents and remove the existing sqllite file.

Now build and test the project.

Play with it, add more entities, more relationships etc.

Read about fetched results sets in the programming docs, and try using that... its really cool.
michelle is offline   Reply With Quote
Old 07-27-2009, 01:33 PM   #8 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 5
Default

Great help. I'm happy using the modeler to add entities, attributes, relationships etc (ps thanks for tip on the gotcha when using simulator), I need to go back over the basics. I'm looking to load a tableview with summary record data from a persistent store and then on row selection show another view with all record data with ability to edit, save and create new. I have most of the jigsaw pieces now, your sample suggestions may be the instructions and big picture I need. Your help most appreciated. I'll let you know when the penny drops and I can run downhill for a change!
Somerset is offline   Reply With Quote
Old 08-07-2009, 12:54 PM   #9 (permalink)
New Member
 
Join Date: Aug 2009
Posts: 2
Default Addin a note field to PhotoLocations

Could anybody please help me figure out how to add a text note field to apples PhotoLocations sample. Or if you can point me to an article that would help. I have no experiance with SQL and have tried to figure it out but no luck so far. Any help would be apreciated. I would like to be able to type a note into a field just above the picture and below the gps location that would be saved to the sql database. I have added a text field that I can edit but it does not save it because I am not sure how to add it to the database.

Thanks again,
Mark
Mark is offline   Reply With Quote
Old 08-07-2009, 01:00 PM   #10 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: michigan
Posts: 184
Default

Quote:
Originally Posted by Mark View Post
Could anybody please help me figure out how to add a text note field to apples PhotoLocations sample. Or if you can point me to an article that would help. I have no experiance with SQL and have tried to figure it out but no luck so far. Any help would be apreciated. I would like to be able to type a note into a field just above the picture and below the gps location that would be saved to the sql database. I have added a text field that I can edit but it does not save it because I am not sure how to add it to the database.

Thanks again,
Mark
go into the modeler , add a an attribute to the photo locations entity. save the model. Make sure the new attribute has been added to the .h header file for the locations entity.
michelle is offline   Reply With Quote
Old 08-07-2009, 09:14 PM   #11 (permalink)
New Member
 
Join Date: Aug 2009
Posts: 2
Default Thanks

Quote:
Originally Posted by michelle View Post
go into the modeler , add a an attribute to the photo locations entity. save the model. Make sure the new attribute has been added to the .h header file for the locations entity.
Thanks I tried that earlier and it didn't work. This time I changed the name of my project so it made a new copy of the app on my iPhone then it worked. So I guess you need to delete the old database when you add to it.
Mark is offline   Reply With Quote
Old 08-08-2009, 04:22 PM   #12 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: michigan
Posts: 184
Default

Quote:
Originally Posted by Mark View Post
Thanks I tried that earlier and it didn't work. This time I changed the name of my project so it made a new copy of the app on my iPhone then it worked. So I guess you need to delete the old database when you add to it.
Sorry, should of mentioned that, but I did early in this thread. Either delete the database or simply rename the reference to it.

When I first started playing with core data I had the same problem you think they would make a point to document that better.

The sqlite database is usually in the documents folder.
michelle is offline   Reply With Quote
Old 09-11-2009, 12:53 PM   #13 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Setauket, NY
Posts: 133
Default

Quote:
Originally Posted by michelle View Post
Sorry, should of mentioned that, but I did early in this thread. Either delete the database or simply rename the reference to it.

When I first started playing with core data I had the same problem you think they would make a point to document that better.

The sqlite database is usually in the documents folder.
Do you know how to create a blank database based on an xcdatamodel? The template in XCode has the logic if the database doesn't exist, you copy a blank one from the application bundle. I just don't know if it's just a place-holder document of .sqlite file-type or is it a real db file based on the model schema. Any help?
Thanks.
hstaniloff is offline   Reply With Quote
Old 09-11-2009, 12:58 PM   #14 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: michigan
Posts: 184
Default

Quote:
Originally Posted by hstaniloff View Post
Do you know how to create a blank database based on an xcdatamodel? The template in XCode has the logic if the database doesn't exist, you copy a blank one from the application bundle. I just don't know if it's just a place-holder document of .sqlite file-type or is it a real db file based on the model schema. Any help?
Thanks.

Well you would need to either delete the one created in the docs directory or give the database a new name and redeploy to the device.
michelle is offline   Reply With Quote
Old 09-11-2009, 01:03 PM   #15 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Setauket, NY
Posts: 133
Default

Quote:
Originally Posted by michelle View Post
Well you would need to either delete the one created in the docs directory or give the database a new name and redeploy to the device.
Hmmm... The reason I am asking is that when I run the app in the simulator, the database file does not exist yet in the documents folder. So as per the template, a blank db is copied from the applications bundle to the documents folder.
This blank db had to be created somehow. And that's my question: How do you initially create a blank database based on your xcdatamodel schema?
hstaniloff is offline   Reply With Quote
Old 10-15-2009, 06:37 PM   #16 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 9
Default Core Data's not that difficult

Quote:
Originally Posted by hstaniloff View Post
Hmmm... The reason I am asking is that when I run the app in the simulator, the database file does not exist yet in the documents folder. So as per the template, a blank db is copied from the applications bundle to the documents folder.
This blank db had to be created somehow. And that's my question: How do you initially create a blank database based on your xcdatamodel schema?
I assume you've figured this out by now, but if you use the Xcode templates to build a Navigation- or Window-based app, Xcode will create an empty DB for you using the name "<yourappname>.sqlite" if it doesn't already exist. The template doesn't copy anything from your app bundle, unless...

...you want to. If you want to copy over a prefilled database, you can do that in the persistentStoreCoordinator getter in your appDelegate file. Assuming you've created a fully fleshed out Core Data app that has data in it and have run it on the iPhone simulator:
  1. Close the app
  2. Look for your app in one of the folders in ~/Library/Application\ Support/iPhoneSimulator/User/Applications
  3. You'll see a lot of folders with UUIDs for names. If your app was the last one run, you can sort the folders by Date Modified to find yours.
  4. Once you find the right folder, look in the Documents folder and you'll see your .sqlite database. Drag it into your project anywhere (I stick mine in the Resources group normally.)
  5. Now in the appDelegate file, replace the persistentStoreCoordinator getter with the following:

Code:
- (NSPersistentStoreCoordinator *) persistentStoreCoordinator
{
    if (persistentStoreCoordinator != nil)
	{
        return persistentStoreCoordinator;
    }
	
	NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:ksDatabaseFilename];
	
#if defined(USE_DEMO_DATABASE)
	// if we're testing, use a pre-built sample database.
	NSFileManager *fileManager = [NSFileManager defaultManager];
	// If the expected store doesn't exist, copy the default store.
	if ( ! [fileManager fileExistsAtPath:storePath])
	{
		NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Travellers" ofType:@"sqlite"];
		if ( defaultStorePath )
		{
			[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
		}
	}
#endif
	
    NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
	
	NSError *error;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
    if ( ! [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error])
    {
        // Handle error
    }
	
    return persistentStoreCoordinator;
}
Obviously, #define USE_DEMO_DATABASE somewhere to use the copy function.

Now your app has a pre-cooked Core Data object context.

--r
rwetmore is offline   Reply With Quote
Reply

Bookmarks

Tags
core data sqlite, database, objects

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 846
20 members and 826 guests
ajaysharma, astalavista, aziz, carpi, cuonglak, DJFrail, ErnestJNguyen, fiftysixty, ilmman, imran_ime4u, inikolaev_25, Ishika Verma, klbaiju, LEARN2MAKE, Morgan72, provalminsk, Shubhank, suksmo, viperfan91
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,295
Threads: 89,032
Posts: 379,809
Top Poster: BrianSlick (7,086)
Welcome to our newest member, y0ut0be44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:57 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0