How to keep data unique in core data?

Discussion in 'iOS Programming' started by mikezang, Apr 25, 2012.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    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?
     
  2. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #2
    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.
     
  3. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    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;
     
  4. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #4
    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.
     
  5. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #5
    Can I design Entity to have unique key like in SQLite?
     
  6. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #6
    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.
     
  7. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #7
    What do you mean make own unique key?
     
  8. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #8
    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.
     
  9. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #9
    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.
     
  10. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #10
    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?
     
  11. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #11
    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.
     

Share This Page