Can I confirm this is the correct use of the chain builder?
I don't wish to build using the dot syntax as certain queries are optional.
Originally created by @markst on GitHub (Nov 27, 2018).
I wish to build the fetch clauses as follows:
```swift
var nameFilter:String? {
didSet {
var chainBuilder = FetchChainBuilder<Customer>()
if nameFilter?.count ?? 0 > 0 {
let customerName = NSPredicate(format: "%K contains[cd] %@", "name", nameFilter!) // \Customer.name
let phonePredicate = NSPredicate(format: "%K contains[cd] %@", "phone", nameFilter!) // \Customer.phone
let emailPredicate = NSPredicate(format: "%K contains[cd] %@", "email", nameFilter!) // \Customer.email
let combinedPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: [customerName,phonePredicate,emailPredicate])
chainBuilder = chainBuilder.where(Where<Customer>(combinedPredicate))
}
switch sortOrder {
case .AtoZ:
chainBuilder = chainBuilder.orderBy(.ascending(\.name))
case .ZtoA:
chainBuilder = chainBuilder.orderBy(.descending(\.name))
}
self.monitor?.refetch(
chainBuilder.fetchClauses
)
}
}
```
Can I confirm this is the correct use of the chain builder?
I don't wish to build using the dot syntax as certain queries are optional.
adam
added the question label 2025-12-29 18:24:48 +01:00
I don't see anything wrong with how you're doing it. Just to add to your arsenal, you can also just store the Where<T> and OrderBy<T> clauses separately:
In your second example, though, I'm not sure why you are returning [FetchClause] and not FetchChainBuilder<Booking>. If you really need just the array of clauses you can use my code above and just say
return[filter,sort]
@JohnEstropia commented on GitHub (Dec 5, 2018):
I don't see anything wrong with how you're doing it. Just to add to your arsenal, you can also just store the `Where<T>` and `OrderBy<T>` clauses separately:
```swift
let filter: Where<Booking>
if self.showWaitlist {
filter = Where(\.date >= self.startDate && \.date < self.endDate)
} else {
filter = Where(\.date >= self.startDate && \.date < self.endDate && \.waitlist == false)
}
let sort: OrderBy<Booking>
switch sortMode {
case .arrivalTime:
sort = OrderBy(.ascending(\.date))
case .customerName:
sort = OrderBy(.ascending("customer.name"))
case .sections:
sort = OrderBy(.ascending(\.tables))
}
let fetchChainBuilder = From<Booking>().where(filter).orderBy(sort)
// ...
```
In your second example, though, I'm not sure why you are returning `[FetchClause]` and not `FetchChainBuilder<Booking>`. If you really need just the array of clauses you can use my code above and just say
```swift
return [filter, sort]
```
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @markst on GitHub (Nov 27, 2018).
I wish to build the fetch clauses as follows:
Can I confirm this is the correct use of the chain builder?
I don't wish to build using the dot syntax as certain queries are optional.
@markst commented on GitHub (Nov 27, 2018):
Here's another example:
@JohnEstropia commented on GitHub (Dec 5, 2018):
I don't see anything wrong with how you're doing it. Just to add to your arsenal, you can also just store the
Where<T>andOrderBy<T>clauses separately:In your second example, though, I'm not sure why you are returning
[FetchClause]and notFetchChainBuilder<Booking>. If you really need just the array of clauses you can use my code above and just say@markst commented on GitHub (Dec 6, 2018):
Thanks John!