No, that is not possible. The only way (as far as I know) to fetch computed results in a Core Data fetch request is using NSExpressionDescription
with [NSExpression expressionForFunction:…]
,
and that does not support the uppercaseString
function. So you have to convert the values
to uppercase after fetching them.
Fetching names from core data and converting it to upper case
题
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"salesRepName == %@",salesName]; [request setPredicate:predicate]; This is my fetching. Is it possible to fetch the name from table and directly converting to UPPER CASE without using a string?
解决方案
其他提示
NSExpression
does support uppercase:
like below:
NSExpression(forFunction: "uppercase:", arguments: [NSExpression(forKeyPath: "name")]
I've used it with NSBatchUpdateRequest
successfully like below to convert all names to uppercase without fetching the records:
let batchUpdateRequest = NSBatchUpdateRequest(entityName: "Person")
batchUpdateRequest.predicate = NSPredicate(value: true)
// Convert name to uppercase
batchUpdateRequest.propertiesToUpdate = ["name": NSExpression(forFunction: "uppercase:", arguments: [NSExpression(forKeyPath: "name")])]
batchUpdateRequest.resultType = .UpdatedObjectIDsResultType
try! self.context.executeRequest(batchUpdateRequest)
不隶属于 StackOverflow