How to check if Core Data has already a value?

I have this code which gets the id and name from one cell.. and then when I select a row I have to save this choice and send it to Core Data... But for example: If I have already a city with a duplicated name.. How I can stop adding it?

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *villesR = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"Ville"
                                   inManagedObjectContext:context];


[villesR setValue:idVille forKey:@"idV"];
[villesR setValue:nomville forKey:@"nom"];

[self.navigationController pushViewController:detailView animated:YES];

Answers


Tidane,

When you need to check if an object is already there or not, you need to set up a fetch request and use a specific predicate. In your case you could simple fetch against Ville and see if you have idV already. I suppose idV is like an identifier for your Ville. Checking the nom could be the same but you could have two cities with the same name in two different places in the world.

@alpz solutions is ok. In addition you could also set the fecth limit to 1:

[request setFetchLimit:1];

Otherwise you could simply do

NSUInteger count = [context countForFetchRequest:request error:&error];    
if(count == 0) {

     // add the new city here
}

instead of

NSArray *results = [context executeFetchRequest:request error:nil]; 

@Phillip Mills is saying that you could use NSManagedObject subclasses instead of using Key Value Coding access. I think it's easier to work with properties of class instead of plain string keys.

So, once you have created a Ville subclass (organising-core-data-for-ios), you could just create a category for it, called Ville+Extension, and create a method like the following:

+ (Ville *)villeWithUniqueName:(NSString *)name withManagedObjectContext:(NSManagedObjectContext*)context
{
    NSFetchRequest * request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"Ville" inManagedObjectContext:context]];
    [request setPredicate:[NSPredicate predicateWithFormat:@"nom = %@", name];
    [request setFetchLimit:1];

    NSArray *results = [context executeFetchRequest:request error:nil];        

    Ville* theVille = nil;  

    if ([results count] == 0)
    {
        theVille = // create the new here
    }
    else
    {
        theVille = (Ville*)[results objectAtIndex:0];
    }

    [request release]; // if you don't use ARC

    return theVille;
}

Then, use that category (after having imported) like:

Ville* theVille = [Ville villeWithUniqueName:@"yourName" withManagedObjectContext:yourContext];

Hope that helps.


You have to check the core data for its existence before you are saving it. This is how I handled the same situation.

NSFetchRequest * request = [[NSFetchRequest alloc] init];
NSManagedObjectContext* context=[Server serverInfo].context;
[request setEntity:[NSEntityDescription entityForName:@"Coupon" inManagedObjectContext:context]];
[request setPredicate:[NSPredicate predicateWithFormat:@"Upc=%@",[[ar objectAtIndex:i]valueForKey:@"upc_code"]]];
NSArray *results = [context executeFetchRequest:request error:nil]; 
[request release];

if ([results count] == 0)
{
    //core data does not have an entry with the upc code(in my case) that i am searching
}
else
{
    // core data have already an entry with that upc code
}

This results array will show you if that entry exists or not. Let me know if you have any queries..happy coding!


Your best bet is probably to create a City class and a class method for it such as:

+ (City *)cityWithUniqueName:(NSString *)name;

In there, you would look up an existing core data object with the name and return it. If it isn't found, create one using insertNewObjectForEntityForName and return that.


Need Your Help

SpringData MongoDB Using projection

spring-data spring-data-mongodb

Is their any way via which I can use mongodb projection feature to fetch selected fields from the document.

How do I group Windows Form radio buttons and textboxes?

c# textbox radio-button grouping

I have 10 textbox controls and 10 radioButton controls, and I have a code similar that, but can I make a group for the textboxes and radioButtons?