fetchRequest doesn't give same result as filterPredicate

  • Hi,

    My Core Data based application has the following data model :
    - a "person" entity
    - a "file" entity with a "downloadedAt" attribute
    - a relationship between the two : person.files <-->> file.person

    What I'd like to have is an array of persons that match the following
    predicate :
    "ALL files.downloadedAt == NULL"

    The predicate works if it's applied to a NSArrayController using :
    [personsController setFilterPredicate:[NSPredicate
    predicateWithFormat:@"ALL files.downloadedAt == NULL"]];

    But it doesn't work when I try to do the same thing with a
    fetchRequest :

    @implementation Person
    - (void)test
    {
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSManagedObjectContext *context = [self managedObjectContext];
    NSError *err = nil;
    NSArray *result;
    [request setEntity:[NSEntityDescription entityForName:@"Person"
    inManagedObjectContext:context]];
    [request setPredicate:[NSPredicate predicateWithFormat:@"ALL
    files.downloadedAt == NULL"]];
    result = [context executeFetchRequest:request error:&err];
    Log(@"%@",result);
    Log(@"%@",err);
    [request release];
    }
    @end

    The following code just logs :
    ()
    (null)

    Meaning the array is empty and there is no error...
    What am I missing ? Why are the two methods giving a different result ?

    Many thanks,
    Martin.
  • Problem solved :

    I just do [context refreshObject:self mergeChanges:YES]; before the
    fetchRequest and things work great.

    Martin.

    > Hi,
    >
    > My Core Data based application has the following data model :
    > - a "person" entity
    > - a "file" entity with a "downloadedAt" attribute
    > - a relationship between the two : person.files <-->> file.person
    >
    > What I'd like to have is an array of persons that match the
    > following predicate :
    > "ALL files.downloadedAt == NULL"
    >
    > The predicate works if it's applied to a NSArrayController using :
    > [personsController setFilterPredicate:[NSPredicate
    > predicateWithFormat:@"ALL files.downloadedAt == NULL"]];
    >
    > But it doesn't work when I try to do the same thing with a
    > fetchRequest :
    >
    > @implementation Person
    > - (void)test
    > {
    > NSFetchRequest *request = [[NSFetchRequest alloc] init];
    > NSManagedObjectContext *context = [self managedObjectContext];
    > NSError *err = nil;
    > NSArray *result;
    > [request setEntity:[NSEntityDescription entityForName:@"Person"
    > inManagedObjectContext:context]];
    > [request setPredicate:[NSPredicate predicateWithFormat:@"ALL
    > files.downloadedAt == NULL"]];
    > result = [context executeFetchRequest:request error:&err];
    > Log(@"%@",result);
    > Log(@"%@",err);
    > [request release];
    > }
    > @end
    >
    > The following code just logs :
    > ()
    > (null)
    >
    > Meaning the array is empty and there is no error...
    > What am I missing ? Why are the two methods giving a different
    > result ?
    >
    > Many thanks,
    > Martin.
    >
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today