Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch object by property in Core Data

In my iPhone project, I want to write a function that checks wether there's an object in my Core Data ManagedObjectContext with a given value for a certain property, say some_property.

If there's already an object with some_property == 12, I want the function to return the object, otherwise, I want to create the object, or at least return nil.

How would I do that?

like image 246
winsmith Avatar asked Sep 17 '09 12:09

winsmith


Video Answer


2 Answers

The following snippet shows how to retrieve the objects matching a specific predicate. If there are no such objects, the snippet shows how to create a new object, save it and return it.

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"YourEntityName" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];
    // retrive the objects with a given value for a certain property
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"property == %@", value];
    [request setPredicate:predicate];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"yourSortKey" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];



    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;

    NSError *error = nil;
    NSArray *result = [managedObjectContext executeFetchRequest:request error:&error];

    [request release];
    [sortDescriptor release];
    [sortDescriptors release];


    if ((result != nil) && ([result count]) && (error == nil)){
         return [NSMutableArray arrayWithArray:result];
    }
    else{
        YourEntityName *object = (YourEntityName *) [NSEntityDescription insertNewObjectForEntityForName:@"YourEntityName" inManagedObjectContext:self.managedObjectContext];
            // setup your object attributes, for instance set its name
            object.name = @"name"

            // save object
            NSError *error;
            if (![[self managedObjectContext] save:&error]) {
             // Handle error
             NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

            }

            return object;

   }
like image 75
Massimo Cafaro Avatar answered Oct 17 '22 04:10

Massimo Cafaro


It's better if you don't do multiple fetching if you want to check for certain properties on the local data. Just do one fetch request using a pre-populated array and then iterate or filter the results.

This is a code snippet from Core Data Programming Guide "Implementing Find-or-Create Efficiently":

// get the names to parse in sorted order
NSArray *employeeIDs = [[listOfIDsAsString componentsSeparatedByString:@"\n"]
        sortedArrayUsingSelector: @selector(compare:)];

// create the fetch request to get all Employees matching the IDs
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:
        [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:aMOC]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat: @"(employeeID IN %@)", employeeIDs]];

// make sure the results are sorted as well
[fetchRequest setSortDescriptors: [NSArray arrayWithObject:
        [[[NSSortDescriptor alloc] initWithKey: @"employeeID"
                ascending:YES] autorelease]]];
// Execute the fetch
NSError *error;
NSArray *employeesMatchingNames = [aMOC
        executeFetchRequest:fetchRequest error:&error];
like image 39
Jesse Armand Avatar answered Oct 17 '22 04:10

Jesse Armand