views:

28

answers:

2

Hi all,

I have to entities namely 'account' and 'event'.

The account has to-many relation to event and inverse to-one from event.

Now when I save a event object to account it is working nicely.

But when I fetch for events for particular related account it gives all the entities available which is not desirable behavior.

I am passing the currentAccount object as follows :

if (PrivarteController == nil) {
        PrivarteController *aController = [[PrivarteController alloc]initWithNibName:@"PrivarteController"
                                                                                          bundle:nil];
        self.PrivarteController = aController;
        [aController release];
    }
    self.PrivarteController.shouldFetchNewCounts = YES;
    self.PrivarteController.currentAccount = [self.fetchedResultsController objectAtIndexPath:indexPath];
    [[self navigationController] pushViewController:self.PrivarteController animated:YES];

in that controller I am fetching for related events to an account like this :

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

    NSString *predicateFormat = [NSString stringWithFormat:@"ddtype = 'Home'"];

    NSPredicate *pred = [NSPredicate predicateWithFormat:predicateFormat];
    [request setPredicate:pred];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"DDEvent" inManagedObjectContext:[self.currentAccount managedObjectContext]];
    [request setEntity:entity];
    [request setResultType:NSManagedObjectResultType];

    NSError *error = nil;
    NSArray *array = [[self.currentAccount managedObjectContext] executeFetchRequest:request error:&error];

In this request suppose I have two events of Home in account A and 2 events of Home in account B so instead of giving me 2 objects for each account it gives me all the 4 objects for both accounts so what is wrong with the query .

I have no clue what is going wrong ?

Thanks ,

+1  A: 

Your predicate must filter on account-- they share a managed object context.

Justin
but the TaggedLocations application of apple is doing the same thing and it is working in their application . or we must filter on account ?
hib
Unless you're maintaining separate contexts (and, presumably, persistent stores), you'll need your predicate to be explicit.
Justin
It just got over to my head . sorry for asking again but what do you mean by explicit predicate ?
hib
I only meant that your predicate must include all of the conditions you want your fetch results to satisfy, including account.
Justin
great man , some how it solves the problem . not actually the way I want it to be but solves as I have to put account name to be unique.
hib
You may want to filter on a new accountId field if the names aren't unique.
Justin
+1  A: 

Not sure I am reading this correctly because you talk about account and event entities but those names don't appear in the code.

Nevertheless, it looks to me like the fetch is working properly. You told it to fetch all DDEvent objects whose ddevent attribute equals 'home'. You say you have four such object and you get four back.

I think your problem is that you have the wrong entity in your fetch. If you want to search based on the account, you need to set the entity of the fetch to the Account entity. Then you tell the predicate to walk the relationships. with a predicate like "events.ddevent=='home'". That will return a list of Account entities with that criteria.

Update:

A fetch returns an array of only one type of object. You will get back either an array of Account objects or an array of DDEvent objects. You need to be clear which object you need.

When you say:

In this request suppose I have two events of Home in account A and 2 events of Home in account B so instead of giving me 2 objects for each account it gives me all the 4 objects for both accounts so what is wrong with the query

You seem to expect to get back both Account and DDEvent objects. Right now, you are fetching only DDEvent objects.

Once you're clear about which object you want, then you need to be clear about what properties the specific objects you want should have. Right now, Right now, you are fetching only DDEvent objects whose attribute 'ddevent=='Home'` without any record to any other attribute or relationship. If you want them to have a relationship to a particular account, then you need to add that to the predicate.

Usually, if you want a particular object at the other end of a relationship you fetch the primary object and then walk it's relationship. In this case, if you wanted the DDEvent objects related to Account object A matching a criteria. You would fetch the Account object A and then you would use a predicate to filter the directdebits set to find the specific DDEvent objects you wanted.

TechZen
its not working . suppose I have relationship named "directdebits" from Account to DDEvent and I am modifying my query like you have said NSString *predicateFormat = [NSString stringWithFormat:@"directdebits.ddtype = 'Home'"];But is giving me the error that the key path doesn't exist .I also don't understand "think your problem is that you have the wrong entity in your fetch. If you want to search based on the account, you need to set the entity of the fetch to the Account entity" as I think I am already doing this so please explain if possible through a small snippet.
hib
I want to search those DDEvents whose ddtype == 'Home' not Account.
hib