Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mikezang

macrumors 6502a
Original poster
May 22, 2010
938
41
Tokyo, Japan
I am using Core data without big problems, but I am not sure how can I keep data in unique, because I don't want to save same data into two objects.

How can I do?
 
When you create a database schema you normalized the data to prevent duplication and optimize.

Could you describe the data you're storing to help understand the problem better.
 
When you create a database schema you normalized the data to prevent duplication and optimize.

Could you describe the data you're storing to help understand the problem better.
My data is as below
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSData * icon;
@property (nonatomic, retain) Detail *detail;
@property (nonatomic, retain) NSSet *tests;
 
So your question is how do I keep from having the same person in the database twice?

Before you Insert a new record you will need to search the database, i'm guessing looking for the name if that is unique, to see if it exists first if it does don't insert the new one.
 
So your question is how do I keep from having the same person in the database twice?

Before you Insert a new record you will need to search the database, i'm guessing looking for the name if that is unique, to see if it exists first if it does don't insert the new one.
Can I design Entity to have unique key like in SQLite?
 
I'm don't recall if there is a way to do this in the model editor, and I don't have access to xcode at the moment.

But if not, you could always generate your own unique key.
 
So add a new attribute to your entity

Code:
@property (nonatomic, retain) NSString * UID;

And generate a unique value when you insert the the data.
 
Each object in your Core data database already have a unique ID - the managed object ID. This will most likely meet your needs.

It's not really clear what you want to do though, do you want to allow several entries with same name for instance or do you want to disallow?

If you want to allow then just work away, you can create as many managed objects as you want using the exact same values and they will still be unique - because core data will keep a managedobjectid for you.

If you don't want to allow more than one 'Eric' in your entire database you must first search for any Eric's before creating a new one. In this case make sure to index the name table.
 
Each object in your Core data database already have a unique ID - the managed object ID. This will most likely meet your needs.

If you don't want to allow more than one 'Eric' in your entire database you must first search for any Eric's before creating a new one. In this case make sure to index the name table.
This is what I need! I will try to do it.
But I am still not sure why Apple only provide such way for unique attribute, I mean they can provide one method to do this so that no search need by user, can you explain that?
 
This is what I need! I will try to do it.
But I am still not sure why Apple only provide such way for unique attribute, I mean they can provide one method to do this so that no search need by user, can you explain that?

I'm pretty comfortable with Core Data so every project I do is using Core Data - even if I just need to store a small bit of data - for the simple reason that it's so darn handy!

To be honest, this has never been a problem for me.

Generally, the use case where you don't want to have duplicate records - you still want to add your data to the database. So you fetch for a record with name 'Eric' if there is one there, you use that record and add to it, otherwise you create a new record.

I don't think I've ever been in the situation where if there is a record already I just discard the incoming data...

To me it makes perfect sense.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.