views:

2282

answers:

5

I am fairly new at programming in Objective-C, having been a windows programmer since I was a kid. I am falling in LOVE with it.

I am, however, having a bit of trouble figuring out this Core Data stuff. How do I create a new entry with a unique ID? In SQL I would just declare one field as an autoincrement field. I'm not seeing anything like that here, but I could just be missing something. I just want an auto incrementing NSInteger field, so when I manually add items to the database, I will have some form of reference to them.

+7  A: 

That ain't how CoreData works.

In CoreData, you create instances of entities. Each instance is unique. You then retrieve and manipulate instances as needed. CoreData takes care of the persistence for you, including uniquely identifying instances.

Step away from everything you know about traditional relational databases.

Start here:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html

Then go here:

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

Specifically, this:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdBasics.html#//apple%5Fref/doc/uid/TP40001650-TP1

CoreData is an awesomely powerful bit of technology that offers features well beyond just database like persistence. It'll save you many lines of code and perform extremely well if you embrace it.

bbum
+5  A: 

Though i agree with what you said, there is an ID mechanism behind core data. ID are indeed managed by Core-Data but you can retrieve them with :

NSManagedObjectID *moID = [managedObject objectID];

For more informations see :

http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/CoreData/Articles/cdUsingMOs.html

+1  A: 

What if we want to sync with a remote database that does need autoincremented id's for unique rows of data? Is there a way to implement autoincremented id's if we need to. I understand it's not required for core data but I need to import data from a remote database and then upload again and make sure the id's are intact.

marchinram
+1  A: 

i think this will be usefull in such conditions.

http://lists.apple.com/archives/cocoa-dev/2006/Jul/msg01801.html We can get maxExistingID . SO we can increment that when saving a new item

Sijo
+1  A: 

take a look at: [[NSProcessInfo processInfo] globallyUniqueString]

Apple docs: Global ID for the process. The ID includes the host name, process ID, and a time stamp, which ensures that the ID is unique for the network.

which is handy if you don't want people guessing the id

yooj