0

I am trying to learn osx and i would like to find out more about core data. I have read some parts of the documentation and some books, and I am now experimenting with core data in general.

Trying to make trivial mac app, i run in the database design issue: Let say that in RDBMS you would have ids, primaryKeys and foreignKeys like this

table products
productID
categoryID
etc....

table Categories
categoryID
etc.....

My questions are:

  1. What is the equivalent of primary key in core data?
  2. What is the eqivalent of the foreign key in core data?
  3. How do you ensure that record is unique in the table?
  4. Can anyone clarify the design concept in core data database?

Any links about core data (aside from Core Data Programming Guide from apple) will be appreciated.

Regards, John

user2417624
  • 613
  • 8
  • 28
  • 1
    If you read the Apple guide you would at least be able to answer your question 4 (the keyword is _object graph_ rather than _database_). Foreign keys are called relationships, the primary key is Core Data's own `objectID` and to make a record unique you have to implement your own validation before saving it. Please ask more specific questions in the future. – Sebastian Wramba Jan 12 '15 at 10:25
  • I guess that all comes with experience. on the beginning even obvious things looks complicated. Can you please post a link with code example of how can I get the objectID which is the primary key? – user2417624 Jan 12 '15 at 10:30
  • 3
    Core Data is not a database. You won't get very far if you think in database terms. Related: http://stackoverflow.com/questions/523482/core-data-vs-sqlite-3/524301#524301 http://stackoverflow.com/questions/4720182/core-data-the-primary-key-id-of-a-row-in-the-database http://stackoverflow.com/questions/901640/core-data-primary-key – Matthias Bauch Jan 12 '15 at 10:36
  • 1
    This question doesn't show any indication of research or effort on behalf of the OP. Please, SO is not here to be your Google – Daniel Galasko Jan 12 '15 at 11:05
  • @user2417624 Core Data has it for internal usage. You should not use the `objectID` for your own purposes, or not until you have a deep understanding of how it is being used by Core Data. However, here you have it: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/index.html#//apple_ref/occ/instp/NSManagedObject/objectID – vilanovi Jan 12 '15 at 14:03

2 Answers2

2

First of all, you must understand that Core Data is a object oriented persistence layer and not simply a database. Here you must switch your thinking from records, joints and queries to object oriented design.

Here I'm trying to answer your questions:

1. What is the equivalent of primary key in core data?

Core Data has its own primary key system. Each object is identified by a unique "ObjectID" that will be used as primary key internally.

You can define as many attributes in your entities and use them as "primary keys". However you won't be able to make your "record" (object) unique as it has no sense that a "object with a specific property is unique". This kind of logic is you who must add it programatically.

2. What is the eqivalent of the foreign key in core data?

A foreign key is represented in Core Data by a relationship. There are one-to-one relationships and one-to-many relationships. This means that an object can have a pointer to another object (therefore, in the database you would have a "foreign key") or an object have a collection that contains other objects (an array for example) (therefore, in the database you would have an extra table to represent this structure).

3. How do you ensure that record is unique in the table?

As mentioned above, you can't. In CoreData you have unique objects (NSManagedObject) per each context (NSManagedObjectContext) but you cannot control that an object with an specific attribute is unique among your other objects.

4. Can anyone clarify the design concept in core data database?

As said in the introduction, here you should not think in terms of databases. Core Data is an oriented object design persistence framework.

Core Data is not a simple thing that is fast learned. You need to spend some time to understand what you can do and most important, why and in which cases you should do it.

I highly recommend you to read the Core Data Programming Guide:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html

Hoping to be helpful,

Joan

vilanovi
  • 2,047
  • 1
  • 21
  • 25
0

If you read the Apple Core Data Programming Guide you can get an idea and clarify your doubts

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdTechnologyOverview.html