views:

115

answers:

3

Hello,

I have the following code in place:

    NSString *mapIDx = @"98";
    NSLog(@"map id: %@", mapIDx);

    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"WayPoint" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    //NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id=%@", mapIDx];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id==%@", mapIDx];
    [request setPredicate:predicate];

    NSError *error;
    listArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    [request release];


    int arrayItemQuantity = [listArray count];
    NSLog(@"Array Quantity: %d", arrayItemQuantity);

    // Loop through the array and display the contents.
    int i;
    for (i = 0; i < arrayItemQuantity; i++)
    {
        NSLog (@"Element %i = %@", i, [listArray objectAtIndex: i]);
    }

    /*
    NSInteger *xCoordinate = listArray[1];
    NSInteger *yCoordinate = listArray[3];
    NSLog(@"xCoordinate: %@", xCoordinate);
    NSLog(@"yCoordinate: %@", yCoordinate);

    CLLocationCoordinate2D coordinate = {xCoordinate, yCoordinate};
    MapPin *pin = [[MapPin alloc]initwithCoordinates:coordinate];
    [self.mapView addAnnotation:pin];
    [pin release];
    */

    [listArray release];

As you can see I'm trying to select specific objects from my database, anything with a waypoint_map_id of 98, but the NSPredicate is not working as I expected. Zero objects are getting selected.

Anyone any thoughts ?

+1  A: 

Assuming that you definately have that object in your database, try adding quotes around the value?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id==\"%@\"", mapIDx];

(clutching at straws!)

Your prediacte looks fine so I would instantly start to suspect the bug is somewhere else :

  • Is there definintely a waypoint with that id?
  • Is listArray nil i.e. something else has gone wrong with the request?

You don't check to see what the error is - perhaps that will give you more information?

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

if (nil == results || nil != error)
  NSLog(@"Error getting results : %@", error);

listArray = [results mutableCopy];

Hope that's helpful at all!

deanWombourne
thanks, I've put in the error checking code and retested, not getting any errors. There are definitely waypoints with that id, when I run without the predicate, I can see the output in the console.
Stephen
What do you get if you run it without the prediacte and output __NSLog(@"%@", item.waypoint_map_id);__ for each item in listArray?
deanWombourne
the following line: NSLog (@"Element %i = %@", i, [[listArray objectAtIndex: i] valueForKey:@"waypoint_map_id"]); outputs 'Element 1813 = 98' to the console.
Stephen
+2  A: 

The predicate with format does not covert the string "98" to a number. Instead it does

waypoint_map_id == "98"

... which is looking for string attribute. Change the predicate to:

NSInteger mapIdx=98;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id==%d", mapIDx];

... which returns a predicate of:

waypoint_map_id == 98
TechZen
thanks, waypoint_map_id is a string in the database.
Stephen
Oh, then are you sure you have a `WayPoint` object with a `waypoint_map_id` attribute of "98"? You might try commenting out the predicate and dumping all the `WapPoint` objects to check.
TechZen
Also, the commented out code looks odd. The first and third elements in the returned array will be `WayPoint` NSManagedObjects (or a subclass). Since you don't have a sort descriptor assigned to the fetch, they will be in random order. Then you're assigning their addresses as pointers to integers and then attempting to use those addresses as doubles in a coordinate definition. That won't work. I think you may have something confused about your objects.
TechZen
A: 

Problem solved:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id contains[cd] %@", mapIDx];
    [request setPredicate:predicate];
Stephen