-
Notifications
You must be signed in to change notification settings - Fork 2
Fetchable
The only requirement to use those fetch functions is to declare the conformance of your Core Data entity to DatabaseEntity :
extension PlayerEntity: DatabaseEntity {}When executing a request, you have the provide the context where the request should be executed. To not pass a context parameter each time you call the fetch function, you can optionally specify a default context to be used for fetching within a Fetchable extension.
extension Fetchable {
var context: NSManagedObjectContext? { /** return your app view context */ }
}You can then either fetch the entity directly, or fetch its DatabaseModel if you declared one. For the examples, we will use the DatabaseModel and assume that a default context is provided.
You can specify a target when fetching.
// fetch all player entities (mapped with the 'Player' database model)
Player.request().all().fetch() // output: [Player]
// fetch the first player entity (mapped with the 'Player' database model)
Player.request().first().fetch() // output: Player?
// fetch the first 10th player entities (mapped with the 'Player' database model)
Player.request().first(nth: 10).fetch() // output: [Player]You can specify a predicate on a single attribute when fetching. When comparing the value, you can use the comparison operator with no comma. For advanced operators, a comma between the KeyPath and the operator is required.
Player.request()
.first()
.where(\.name == "Zerator")
Player.request()
.first()
.where(\.name, .hasPrefix("Z"))
Player.request()
.all()
.where(\.age >= 20)
Player.request()
.all()
.where(\.score, .isIn(1000...5000))
Player.request()
.first(nth: 10)
.where(\.name, .isIn("Zerator", "Mister MV", "Maghla"))If needed, you can use compound predicates.
Player.request()
.first()
.where(\.name == "Zerator").or(\.name == "Mister MV")
Player.request()
.all()
.where(\.age >= 20).and(\.name, .hasSuffix("ra")).or(\.score < 20)Note about compound nested predicates
It is not possible for now to specify nested predicates. Thus, the evaluation is flat. An issue is open to deal with this feature.
Pass one or several sorts when fetching.
Player.request()
.all()
.where(\.age >= 20)
.sorted(by: .ascending(\.score))
Player.request
.all()
.where(\.name, isIn: ["Zerator", "Mister MV", "Maghla"])
.sorted(by: .descending(\.age), .ascending(\.name))Because you might want to set additional properties of the request, you can use the setting(:to:) function. Also, we think it's a simpler solution rather than writing a function for each property of the request.
Player.request()
.all()
.where(\.age >= 20)
.sorted(by: .ascending(\.score))
.setting(\.returnsDistinctResults, to: true)==, !=, <,<=, >, >=
isIn(:), isNot(in:) to filter the value if contained in the given array or variadic values
hasPrefix, hasNoPrefix, hasSuffix, contains, doesNotContain, and matches(a regular expression)
and their opposites:
hasNoPrefix, hasNoSuffix, doesNotContain, and doesNotMatch
isIn to filter the value if contained in a range (closed or half-open) and isNotIn.
Wiki valid for version 1.0.0