Search code examples
swiftcore-datasubquerynspredicateeditornspredicateeditorrow

NSPredicateEditor with Core Data relationship


I have a core data data model with two entities, Document and Field:

  • Document:

    • fields [to many relationship]
  • Field:

    • name
    • fieldText
    • fieldDate
    • fieldNumber
    • document [to one relationship]

I am trying to set up an NSPredicateEditor to allow someone to filter documents based on the content of certain fields. I manually build the predicate editor like so:

func updatePredicateEditor() {
    print("updatePredicateEditor")

    let sortDescriptor = NSSortDescriptor(key: "orderIndex", ascending: true)
    fieldsArrayController.sortDescriptors = [sortDescriptor]
    let fields = fieldsArrayController.arrangedObjects as! [Field]

    var keyPathsStringArray = [NSExpression]()
    var stringFieldNames = [String]()
    var keyPathsDateArray = [NSExpression]()
    var dateFieldNames = [String]()
    var keyPathsNumberArray = [NSExpression]()
    var numberFieldNames = [String]()

    for i in 0..<fields.count {
        let currentField = fields[i] 

        switch currentField.type! {
        case "Text":
            keyPathsStringArray.append(NSExpression(forKeyPath: "fieldText"))
            stringFieldNames.append(currentField.name!)
        case "Date":
            keyPathsDateArray.append(NSExpression(forKeyPath: "fieldDate"))
            dateFieldNames.append(currentField.name!)
        case "Number":
            keyPathsNumberArray.append(NSExpression(forKeyPath: "fieldNumber"))
            numberFieldNames.append(currentField.name!)
        default:
            print("error on field type")
        }
    }

    let stringOperators = [NSNumber(unsignedInteger: NSPredicateOperatorType.ContainsPredicateOperatorType.rawValue),
                           NSNumber(unsignedInteger: NSPredicateOperatorType.EqualToPredicateOperatorType.rawValue),
                           NSNumber(unsignedInteger: NSPredicateOperatorType.NotEqualToPredicateOperatorType.rawValue),
                           NSNumber(unsignedInteger: NSPredicateOperatorType.BeginsWithPredicateOperatorType.rawValue),
                           NSNumber(unsignedInteger: NSPredicateOperatorType.EndsWithPredicateOperatorType.rawValue)]

    let numberOperators = [NSNumber(unsignedInteger: NSPredicateOperatorType.EqualToPredicateOperatorType.rawValue),
                           NSNumber(unsignedInteger: NSPredicateOperatorType.NotEqualToPredicateOperatorType.rawValue),
                           NSNumber(unsignedInteger: NSPredicateOperatorType.LessThanPredicateOperatorType.rawValue),
                           NSNumber(unsignedInteger: NSPredicateOperatorType.LessThanOrEqualToPredicateOperatorType.rawValue),
                           NSNumber(unsignedInteger: NSPredicateOperatorType.GreaterThanPredicateOperatorType.rawValue),
                           NSNumber(unsignedInteger: NSPredicateOperatorType.GreaterThanOrEqualToPredicateOperatorType.rawValue)]


    let dateOperators = [NSNumber(unsignedInteger: NSPredicateOperatorType.EqualToPredicateOperatorType.rawValue),
                         NSNumber(unsignedInteger: NSPredicateOperatorType.NotEqualToPredicateOperatorType.rawValue),
                         NSNumber(unsignedInteger: NSPredicateOperatorType.LessThanPredicateOperatorType.rawValue),
                         NSNumber(unsignedInteger: NSPredicateOperatorType.LessThanOrEqualToPredicateOperatorType.rawValue),
                         NSNumber(unsignedInteger: NSPredicateOperatorType.GreaterThanPredicateOperatorType.rawValue),
                         NSNumber(unsignedInteger: NSPredicateOperatorType.GreaterThanOrEqualToPredicateOperatorType.rawValue)]

    var rowTemplatesTemp = [NSPredicateEditorRowTemplate]() // this is a temp array to hold the different popupbuttons

    // add a template for Strings

    let leftExpressionStringButton : NSPopUpButton

    if keyPathsStringArray.count == 0 {
        print("There aren't any text fields in NSPredicateEditor")
    }
    else {
        let stringTemplate = NSPredicateEditorRowTemplate(leftExpressions: keyPathsStringArray,
                                                                                         rightExpressionAttributeType: NSAttributeType.StringAttributeType,
                                                                                         modifier: NSComparisonPredicateModifier.DirectPredicateModifier,
                                                                                         operators: stringOperators,
                                                                                         options: (Int(NSComparisonPredicateOptions.CaseInsensitivePredicateOption.rawValue) |
                                                                                            Int(NSComparisonPredicateOptions.DiacriticInsensitivePredicateOption.rawValue)))

        leftExpressionStringButton = stringTemplate.templateViews[0] as! NSPopUpButton

        let stringButtonArray = leftExpressionStringButton.itemTitles

        for i in 0..<stringButtonArray.count {
            (leftExpressionStringButton.itemAtIndex(i)! as NSMenuItem).title = stringFieldNames[i] // set button menu names
        }

        rowTemplatesTemp.append(stringTemplate)
    }

    // add another template for Numbers...

    let leftExpressionNumberButton : NSPopUpButton

    if keyPathsNumberArray.count == 0 {
        print("There aren't any number fields in NSPredicateEditor")
    }
    else {
        let numberTemplate = NSPredicateEditorRowTemplate(leftExpressions: keyPathsNumberArray,
                                                                                         rightExpressionAttributeType: NSAttributeType.Integer32AttributeType,
                                                                                         modifier: NSComparisonPredicateModifier.DirectPredicateModifier,
                                                                                         operators: numberOperators,
                                                                                         options: 0)

        leftExpressionNumberButton = numberTemplate.templateViews[0] as! NSPopUpButton

        let numberButtonArray = leftExpressionNumberButton.itemTitles

        for i in 0..<numberButtonArray.count {
            (leftExpressionNumberButton.itemAtIndex(i)! as NSMenuItem).title = numberFieldNames[i] // set button menu names
        }

        rowTemplatesTemp.append(numberTemplate)
    }

    // add another template for Dates...

    let leftExpressionDateButton : NSPopUpButton

    if keyPathsDateArray.count == 0 {
        print("There aren't any date fields in NSPredicateEditor")
    }
    else {
        let dateTemplate = NSPredicateEditorRowTemplate(leftExpressions: keyPathsDateArray,
                                                        rightExpressionAttributeType: NSAttributeType.DateAttributeType,
                                                        modifier: NSComparisonPredicateModifier.DirectPredicateModifier,
                                                        operators: dateOperators,
                                                        options: 0)


        leftExpressionDateButton = dateTemplate.templateViews[0] as! NSPopUpButton

        let dateButtonArray = leftExpressionDateButton.itemTitles

        for i in 0..<dateButtonArray.count {
            (leftExpressionDateButton.itemAtIndex(i)! as NSMenuItem).title = dateFieldNames[i] // set button menu names
        }

        rowTemplatesTemp.append(dateTemplate)
    }

    // create the any, all or none thing...
    let compoundTypes = [NSNumber.init(unsignedInteger: NSCompoundPredicateType.OrPredicateType.rawValue),
                         NSNumber.init(unsignedInteger: NSCompoundPredicateType.AndPredicateType.rawValue),
                         NSNumber.init(unsignedInteger: NSCompoundPredicateType.NotPredicateType.rawValue)]

    // add the compoundtypes
    let compoundTemplate = NSPredicateEditorRowTemplate(compoundTypes: compoundTypes)
    rowTemplatesTemp.append(compoundTemplate)

    print("setting row templates \(rowTemplatesTemp)")

    predicateEditor.rowTemplates = rowTemplatesTemp

    predicateEditor.addRow(self)

}

The problem is that NSPredicateEditor does not allow NSExpressions with NSSubqueryExpressionType, so I can't have a subquery as my left expression. I need the NSPredicateEditorRowTemplate to include both the field name as well as the attribute path (fieldText, fieldDate or fieldNumber), so that the final predicate from one row would look like:

name == FIELDNAME && fieldText CONTAINS "<entered text>"

Any suggestions for how to do this? Should I be subclassing NSPredicateEditorRowTemplate, and if so how? Thanks in advance for any help.


Solution

  • Subclass NSPredicateEditorRowTemplate and override predicateWithSubpredicates to convert predicate "FIELDNAME CONTAINS <entered text>" to predicate "name == FIELDNAME && fieldText CONTAINS <entered text>".