使用存储在 CoreData 模型中的 FetchRequest 进行变量替换

发布于 2024-09-03 02:48:36 字数 877 浏览 11 评论 0原文

我总是完全在代码中创建 NSFetchRequests。现在我正在查看 Xcode GUI 来构建获取请求并将其存储在模型中。

我正在遵循 Xcode 文档中的示例。我向我的模型添加了一个 Fetch Request,通过建模 GUI 创建的谓词是:

 firstName LIKE[c] "*SUBSTRING*"

然后我用这两行检索该请求:

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

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

生成的 NSFetchRequest 的 NSLog 输出如下:

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

.. 这表明该变量不是在存储的 FetchRequest 返回之前被替换。

那么,如何指定在 Xcode 数据建模 Fetch Request Predicate Builder GUI 中输入的文本在运行时被 NSFetchRequest:fetchRequestFromTemplateWithName:substitutionVariables: 替换?

谢谢你!

伍迪

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓酥 2024-09-10 02:48:36

您需要右键单击包含预期变量的获取请求谓词编辑器的行,然后从弹出窗口中选择“VARIABLE”。在 Xcode 编辑器中右键单击的位置有时很挑剔,因此我倾向于单击 +/- 按钮的左侧。

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.

-黛色若梦 2024-09-10 02:48:36

这是替换变量的示例。

首先在获取请求部分创建 fetchRequest 模板。

输入图片description here

然后编写通过名字获取员工的代码。

   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
   }

Here is the example of substitution Variables.

First create the fetchRequest Template in the Fetch Requests Section.

enter image description here

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
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文