Variable Substitution with FetchRequests stored in a CoreData Model

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 04:22:06

问题


I've always created my NSFetchRequests entirely in-code. Now I'm looking at the Xcode GUI for building a fetch request and storing it in the model.

I'm following an example from the Xcode Documentation. I added a Fetch Request to my model, and the predicate that has been created through the Modelling GUI is:

 firstName LIKE[c] "*SUBSTRING*"

I then retrieve that request with these two lines:

NSDictionary *substituionDictionary = [NSDictionary dictionaryWithObject:@"woody" forKey:@"SUBSTRING"];

NSFetchRequest *fetchRequest = [mom fetchRequestFromTemplateWithName:@"firstNameContains" substitutionVariables:substituionDictionary];

An NSLog of the resulting NSFetchRequest outputs this:

(entity: Customer; predicate: (firstName LIKE[c] "*SUBSTRING*"); sortDescriptors: (null); limit: 0)

.. which indicates that the variable is not being substituted prior to the return of the stored FetchRequest.

So, how does one specify that text entered in the Xcode Data Modelling Fetch Request Predicate Builder GUI is intended to be substituted at runtime by NSFetchRequest:fetchRequestFromTemplateWithName:substitutionVariables: ?

Thank you!

Woody


回答1:


You need to right-click on the row of the fetch request predicate editor containing the intended variable and select "VARIABLE" from the popup. Where you right-click is sometimes picky in the Xcode editor, so I tend to click just to the left of the +/- buttons.




回答2:


Here is the example of substitution Variables.

First create the fetchRequest Template in the Fetch Requests Section.

Then write the code for fetch employee by firstName.

   func employeeByFirstName(fName: String) -> [Employee]{

    let viewContext = self.persistentContainer.viewContext

    var substitutionVariables: [String: Any] = [String : Any]()
    substitutionVariables["FIRSTNAME"] = fName
    let fetchRequest = fetchRequestBy(name: "fetchByFirstName", variables: substitutionVariables) as! NSFetchRequest<Employee>

    do {
        let objects = try viewContext.fetch(fetchRequest)
        return objects
    } catch  {
        let nserror = error as NSError
        fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
    }
}


   func fetchRequestBy(name: String, variables: [String : Any]) -> NSFetchRequest<NSFetchRequestResult>? {
      let model = self.persistentContainer.managedObjectModel
      let fetchRequest = model.fetchRequestFromTemplate(withName: name, substitutionVariables: variables)
      return fetchRequest
   }


来源:https://stackoverflow.com/questions/2897068/variable-substitution-with-fetchrequests-stored-in-a-coredata-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!