Querying an NSSet from CoreData
So... for entities that have a to-many relationship you get an NSSet property. When querying this property you can use the ANY and ALL operators.
Scenario:
{
You have entity A that has a to-many relationship with entity B.
Entity B has two rows. One having a property that is set to FALSE, the second row has that same property set to TRUE.
Entity A has two rows. The first having a link to the FALSE row, the second having a link to both TRUE & FALSE rows.
}
Selecting rows from entity A where "ANY" of the rows from entity B has the property set to FALSE, will return both rows.
Selecting rows from entity A where "ALL" of the rows from entity B has the property set to FALSE, will return only row one from entity A. As this is the only row where ALL the links are FALSE.
THE PROBLEM:
The "ALL" operator doesn't work! It will crash your application.
You will get the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate (null)'
Which sends you on a wild goose chase, trying to figure out why your predicate is null. It's not really null. This is a bug.
See reference here at stackoverflow
THE FIX (HACK/WORKAROUND):
This is where our old friend De Morgan comes into play. As the ANY operator actually works, we have to negate our previous predicate.
FOR EXAMPLE
Thus going from this:
"ALL relationshipToEntityBName.someProperty == 1"
To this:
"NOT (ANY relationshipToEntityBName.someProperty != 1)"
Scenario:
{
You have entity A that has a to-many relationship with entity B.
Entity B has two rows. One having a property that is set to FALSE, the second row has that same property set to TRUE.
Entity A has two rows. The first having a link to the FALSE row, the second having a link to both TRUE & FALSE rows.
}
Selecting rows from entity A where "ANY" of the rows from entity B has the property set to FALSE, will return both rows.
Selecting rows from entity A where "ALL" of the rows from entity B has the property set to FALSE, will return only row one from entity A. As this is the only row where ALL the links are FALSE.
THE PROBLEM:
The "ALL" operator doesn't work! It will crash your application.
You will get the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate (null)'
Which sends you on a wild goose chase, trying to figure out why your predicate is null. It's not really null. This is a bug.
See reference here at stackoverflow
THE FIX (HACK/WORKAROUND):
This is where our old friend De Morgan comes into play. As the ANY operator actually works, we have to negate our previous predicate.
FOR EXAMPLE
Thus going from this:
"ALL relationshipToEntityBName.someProperty == 1"
To this:
"NOT (ANY relationshipToEntityBName.someProperty != 1)"
Comments
Post a Comment