views:

90

answers:

0

I'm using a simple NSComparisonPredicate similar to: ("employeeDepartment != %@", department) where employeeDepartment is an optional, one-to-many relationship in Employees, and department is an NSManagedObject of Departments. When applied against the Employees entity, I'm getting different results from an NSArrayController vs an NSFetchRequest when employeeDepartment is nil (in my actual data model nil values are allowed since employeeDepartment is optional).

When I set this predicate on the array controller it includes Employees that have a nil employeeDepartment. When used in the fetch request, is excludes those same Employees. The former handling (where nil values are included) is my desired outcome. I've tried various settings on the NSFetchRequest, but can't get it to include the nil values. The Core Data store is SQL based.

NSPredicate * predicate = [NSPredicate predicateWithFormat: @"employeeDepartment != %@", department];

// arrangedObjects will include the nil values
[arrayController setFilterPredicate: predicate]; 

// employees array will exclude the nil values
NSFetchRequest * request = [[NSFetchRequest alloc] init];
// ... (set context and entity)
[request setPredicate: predicate];
NSArray * employees = [context executeFetchRequest: request error: &error];

Adding a compound OR that includes nil values produces the desired results, but makes a mess for handling in a NSPredicateEditorRowTemplate (employeeDepartment != department OR employeeDepartment = nil).

Is there a tip or preferred workaround to get the fetch request to include the nil values without requiring a compound predicate? Is there an intended reason for the different behavior from controllers vs fetch requests?