views:

304

answers:

3

Hi all!

Been having a bit of a headache with this one and was wondering if someone could offer some advice!

Im implementing persistent storage with Core Data.

I have two Entities Car and Driver. A car can have many drivers.

  • In one view i am collecting the drivers and storing them to a NSMutableArray

  • This array is getting passed to another view and at some point i will be saving it against a Car

What i would like to do is save each driver from the array in a seperate row in the table of drivers (Just their name (NSString) which will be assigned to a Car.

  • I can create an entry in the database for Car but am struggling with saving each driver in a seperate row in the driver table assigned to that car.

Any suggestions or pointers in the right direction would be greatly appreciated!

Cheers :)

A: 

i'm not a pro in using core data, but you can try to get data from your array using NSKeyArchiver and save returned NSData object to your database. it works fine with sqllite databases.

Morion
+1  A: 

It would be difficult to encapsulate a complete solution to your answer, as your question is fairly broad without seeing your code.

Try to walk through Apple's excellent Books and Recipes sample applications, which cover the Core Data concepts you'll need to progress with your own project.

Alex Reynolds
Very helpful comment thank you... i am well on my way now :)
Tom G
+1  A: 

You mention that you have both Car and Driver entities, yet it sounds like you're trying to save your driver names directly as attributes of a car. Why are you just saving their NSString *name to the car?

Instead of doing that, you should simply update the relationships between your car and its drivers. If you implement your entities as custom classes (and I highly recommend it - see this ADC guide), then when you want to update who drives what car, you can simply say: [car addDriversObject:driver] or [car removeDriversObject:driver].

Like Alex said, you may want to ensure you have a firm grasp on Core Data before heading out on your own too far.

Matt B.
Thank you very much for your input... very much appreciated :)
Tom G