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.
我用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
在一个视图中,我正在收集驱动程序并将它们存储到NSMutableArray中
-
This array is getting passed to another view and at some point i will be saving it against a Car
这个数组被传递到另一个视图,在某些时候我会将它保存在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.
我想要做的是将每个驱动程序从数组中保存在驱动程序表中的单独行中(只是它们的名称(NSString),它将被分配给一个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.
我可以在Car的数据库中创建一个条目,但我正在努力将每个驱动程序保存在分配给该汽车的驱动程序表中的单独行中。
Any suggestions or pointers in the right direction would be greatly appreciated!
任何有关正确方向的建议或指示都将不胜感激!
Cheers :)
3 个解决方案
#1
3
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.
尝试浏览Apple优秀的Books and Recipes示例应用程序,其中包含您需要使用自己的项目进行的核心数据概念。
#2
2
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?
你提到你有Car和Driver实体,但听起来你正试图将你的驱动程序名称直接保存为汽车的属性。你为什么只把他们的NSString *名字保存到汽车里?
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]
.
您应该简单地更新汽车与其驱动程序之间的关系,而不是这样做。如果您将实体实现为自定义类(我强烈推荐它 - 请参阅此ADC指南),那么当您想要更新驱动汽车的人时,您可以简单地说:[car addDriversObject:driver]或[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.
就像亚历克斯所说的那样,你可能希望确保自己掌握核心数据,然后再自行离开。
#3
1
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.
我不是专业人士使用核心数据,但你可以尝试使用NSKeyArchiver从数组中获取数据,并将返回的NSData对象保存到数据库中。它适用于sqllite数据库。
#1
3
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.
尝试浏览Apple优秀的Books and Recipes示例应用程序,其中包含您需要使用自己的项目进行的核心数据概念。
#2
2
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?
你提到你有Car和Driver实体,但听起来你正试图将你的驱动程序名称直接保存为汽车的属性。你为什么只把他们的NSString *名字保存到汽车里?
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]
.
您应该简单地更新汽车与其驱动程序之间的关系,而不是这样做。如果您将实体实现为自定义类(我强烈推荐它 - 请参阅此ADC指南),那么当您想要更新驱动汽车的人时,您可以简单地说:[car addDriversObject:driver]或[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.
就像亚历克斯所说的那样,你可能希望确保自己掌握核心数据,然后再自行离开。
#3
1
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.
我不是专业人士使用核心数据,但你可以尝试使用NSKeyArchiver从数组中获取数据,并将返回的NSData对象保存到数据库中。它适用于sqllite数据库。