Wrapper build above NSPredicate adding object access to NSManagedObjects and request chaining.
Writing magic strings of NSPredicate is not optimal, I've wanted the same experience from Core Data as for example C# has from LINQ in EntityFramework. Objective access guards types you can or can't operate with, suggests only relevant operation choices after every request in the chain and removes a possibility of typos. Framework is lightweight and well documented.
NSPredicateObjectMapper is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'NSPredicateObjectMapper'
To run the example project, clone the repo, and run pod install
from the Example directory first.
Let's have an entity Car, with attributes id, brand, ... I want to fetch all cars with brand starting with "s" case insensitive
fetchRequest.predicate = NSPredicate(format: "brand BEGINSWITH[c] 's'")
and with object wrapper
fetchRequest.predicate = Car.where(\Car.brand).beginsWith("s", caseInsensitive: true).predicate()
chain requests
fetchRequest.predicate = Car.where(\Car.brand).beginsWith("s").and.where(\Car.brand).endsWith("a").predicate()
Swift >= 5.0
- CocoaPods distribution
- Support of SUBQUERIES (SUBQUERY(tasks, $task, $task.completionDate != nil AND $task.user = 'Alex') .@count > 0")
- Support of array operations (array[index], array[FIRST], ...)
- Support of collection queries (@avg, @count, ...)
Ondřej Pišín, [email protected]
NSPredicateObjectMapper is available under the MIT license. See the LICENSE file for more info.