0

I have a Entity with a column of type ID named "responsibleUsers". In this column I store an Array containing NSNumbers. I want to fetch all objects of this entity, that match my current User. Therefore i create following predicate:

    [NSPredicate predicateWithFormat: @"%@ IN responsibleUsers",[NSNumber numberWithInteger: curUser.oID.integerValue] ]

whatever I try, my App crashes. Once with a EXC_BAD_ACESS, once with "unimplemented SQL generation for predicate nsnumber"

What is the correct way to query my entity?

Dominic Sander
  • 2,714
  • 1
  • 18
  • 29

1 Answers1

1

The query you are trying assumes that you have two entities: the entity you querying (let's call it Group) and another one, perhaps called User, which is set up as a to-many relationship from Group called responsibleUsers. You would then be able to use the predicate you suggest:

[NSPredicate predicateWithFormat:@"%@ IN responsibleUsers, aUser]; 

This would be the recommended use of Core Data object graphs.

In your setup, it seems you have an array of NSNumber set as a property rather than a relationship. Therefore you cannot use such a query. You simply have to retrieve the array and query the array.

BOOL containsResponsibleUser = NO;
for (NSNumber *n in aGroup.responsibleUsers) {
    if ([n isEqualTo:[NSNumber numberWithInteger: curUser.oID.integerValue]]) 
       containsResponsibleUser = YES;
}

If you are indeed querying something like a group of users, I would recommend the first approach. If you are querying some kind of user, I would suggest a BOOL property responsible as the most efficient solution.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Thanks - Yes, you are absolutely right. Storing NSArray within CoreData-Transformable-Property is against the nature of Core Data. I changed my model and code towards the usual relationship-mode and everything works finde... thank you. – Dominic Sander Apr 28 '12 at 17:11