简体   繁体   中英

Inserting objects — Core Data

I have just started using Core Data and I am now having trouble understanding how to insert objects. I have the following structure:

  • Location
  • Environment
  • Log

Log, which is the main "row" has a many-to-many relationship to both Environment and Location. And those entities also has a many-to-many relationship to the Log entity.

Both Location and Environment only has one simple attribute. The Log entity stores time, date and some other data.

However I do not understand how do insert process work? I want to make a Log object, that sets the data already mentioned as-well as the locations and the environments. How should I do this?

And I also wonder how to Core Data stores the Location and Environment and links it to a Log.

Because both the location and environment entity will only have maximum four objects in it. So I suppose that the reason why I use NSSet is because the data wont be duplicated? For in SQL these two tables would have been filled with the four possible values (Location): "temp1", "temp2", "temp3", "temp4" and those would have a foreign key in the mapping table so that the values would not be duplicated.

If that makes any sense I would appreciate help from you guys. Thank you for your time.

As has been written many times before, Core Data is not the same as SQL. Core Data is a much richer object model API that happens, in some cases, to use SQLite as a backing store.

That said, you should take a look at some of Apple's Sample Code for examples of Core Data programming. You will see the mantra that is necessary to setup various Core Data instances (eg, NSPersistentStoreCoordinator, NSManagedObjectContext). And you will also see how new NSManagedObject instances are created.

You might also look at the Core Data Programming Guide , if you haven't already.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM