0

I have entity called "Student" which has column called "firstName" and "lastName". My Existing entity looks like this

firstName lastName
Peter Morgan
Adam Hayden
Andrew Giles

Now, I have created a new attribute called "compositeName" which should contain joint name like "Peter Morgan", "Adam Hayden". How can I achieve this with NSBatchUpdateRequest in core data?

After batch update entity should look like this

firstName lastName compositeName
Peter Morgan Peter Morgan
Adam Hayden Adam Hayden
Andrew Giles Andrew Giles

I searched for tutorials on web but I couldn't find one with dynamic values. I found solution in which NSBatchUpdateRequest contains static value

1 Answers1

0

You can use dynamic values via NSExpression in the propertiesToUpdate part of the expression, but unfortunately it looks like it isn't possible to perform a string concatenation using NSExpression.

I would question if you need this additional property at all - why not make it a calculated variable on the managed object:

extension Student {
    var compositeName: String { firstName + lastName }
}

This prevents you ending up with the composite name and name components getting out of sync.

An alternative approach would be to look at derived properties in Core Data, but again, they are NSExpression based, so string concatenation is not available.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thanks a lot @jrturton .. I was just studying for NSBatchUpdateRequest. Most of the tutorial were updating static values with NSBatchUpdateRequest for multiple records in an entity. I was having doubt about handling dynamic values with NSBatchUpdateRequest, so I posted a question with one random example. Again, Thank you so much for your reply . – Yagnesh Londhe Nov 25 '22 at 06:46