Core Data Query Interface (CDQI) is a type-safe, fluent, intuitive library for working with Core Data in Swift. CDQI tremendously reduces the amount of code needed to do Core Data, and dramatically improves readability and refactoring by allowing method chaining and by eliminating magic strings.
CDQI uses the PredicateQI (PQI) package. PQI provides a type-safe Swift interface on top of Apple's NSPredicate
language. CDQI adds the machinery needed to make PQI work with Core Data.
- Fluent interface, i.e., chainable methods
- Large number of useful overloads
- Type-safety — really, type guidance (see below) — in filter comparisons.
- Filtering, sorting, grouping, aggregate expressions, limits, etc.
- Optionally eliminates the use of magic strings so common in Core Data
- Query reuse, i.e., no side-effects from chaining
- macOS 12+, iOS 15+, tvOS 15+, watchOS 8+
- Swift 5.7
The best way to understand how to use CDQI is to look at the unit tests, but an example will make things clear.
First, let's start with the following data model:
class Language: NSManagedObject {
@NSManaged var name: String
@NSManaged var developers: Set<Developer>
}
class Developer: NSManagedObject {
@NSManaged var firstName: String
@NSManaged var lastName: String
@NSManaged var languages: Set<Language>
}
Given this data model, we can start making some queries. But first, remember to import PredicateQI
! Without this import, the compiler magic that allows the comparison expressions to be translated into the NSPredicate
language won't work and in most cases you'll get mysterious compilation errors.
import CoreDataQueryInterface
import PredicateQI
// Which languages are known by at least two of the developers?
// developers.@count >= 2
Query(Language.self).filter { $0.developers.pqiCount >= 2 }
// Which languages are known by developers whose name contains 's'?
// ANY developers.lastname LIKE[c] '*s*'
Query(Language.self).filter { any(ci($0.developers.lastName %* "*s*")) }
We can get the NSFetchRequest
produced by the query by asking for its fetchRequest
property. But it's usually easier just to execute the fetch request directly:
import CoreDataQueryInterface
import PredicateQI
let cooldevs = try Query(Developer.self)
.filter {
// ANY languages.name IN {"Rust","Haskell"}
any($0.languages.name <~| ["Rust", "Haskell"])
}
.fetch(moc)