UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Core Data: Fetch Requests Templates in the XCDataModelD. How to substitute values for placeholders?

Forums > Swift

So we all use the core data "data model" file to create entities, their properties, and their relationships. Paul has several great articles and videos on how to do this.

Did you know that you can also embed fetch requests as templates into the data model?

Not sure how to provide graphic here. I am not clever enough to figure this out.

Then you can use the method fetchRequestFromTemplate(withName:) to pull the template from the model and execute it. I don't see this feature discussed in blogs or in 'how to' videos.

This is where I am stumped. I would like to have a template in the core data 'data model' file. I'd like the template to hold a variable, in this example called "NAME".

Then, in code, I want to pull text from a TextField and pass it TO THE TEMPLATE. I'd like the template to substitute my text value for the placeholder (NAME) and execute the fetchRequest.

How? How do I substitute my variable for the placeholder? This should be easy, but I am not clever enough to figure this out.

func getByName(name: String) {
      let substitute = ["NAME" : name]  // HOW DO I DO THIS PART ?      NAME   or $NAME  
      let byName = getFetchRequest(withName: "byName", variables: substitute) as? NSFetchRequest<Group>
      do {
          groups = try manager.context.fetch(byName!)
      } catch let error {
          print("Bungled the fetch template. \(error.localizedDescription)")
      }
  }

// Works great with templates that have hard coded values. But, I'd like to provide values via placeholders....
func getFetchRequest(withName name: String, variables: [String : Any]) -> NSFetchRequest<NSFetchRequestResult>? {
      // mom is the Managed Object Model
      // this should select the template from the data model and substitute values for the placeholders.
      // This line should return a fetch request, but fails.
      let fetchRequest = mom.fetchRequestFromTemplate(withName: name, substitutionVariables: variables)
      return fetchRequest
  }

2      

Paul wrote an excellent piece asking Apple to provide code examples for all their APIs. This would have helped a great deal. Reimagining Apple's Documentation

Here's the solution I found.

First, you can create predicates in the core data modeler using pick lists. Very easy to say you want all states that begin with "New". Or all sodas that contain "Cola". But these are static fetch requests.

What if you'd like to insert a varible?

In this case, you need to select the Custom Predicate in the data modeler. Then you have to craft a predicate that conforms to NSPredicate syntax AND holds your variables.

Here's an example:

name BEGINSWITH $ARTISTS_NAME

Please note the variable begins with a $.

Now that this template is stored in your xcdatamodeld file in your project, you can extract the fetch request template with code, and substitute variables harvested from your awesome SwiftUI interface! Sweet!

let fetchTemplate = "byArtist" // this is the name in the xcdatamodeld file.
let subs = ["ARTISTS_NAME" : "Taylor Swift"] // Paul's favorite example

let fetchRequest = mom.fetchRequestFromTemplate(withName: fetchTemplate, substitutionVariables: subs)

print ("Predicate is: \(fetchRequest.predicate)")

This will return a proper fetchRequest with the predicate: name BEGINSWITH "Taylor Swift"

Please Note! You must include the $ before the variable name IN THE xcdatamodeld file. BUT you do NOT include the $ in the substitution dictionary. Not sure why. I hope a core data boffin might explain this? Seems a relic leftover from printf days.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.