Correct use of FetchChainBuilder? #243

Closed
opened 2025-12-29 15:27:19 +01:00 by adam · 3 comments
Owner

Originally created by @markst on GitHub (Nov 27, 2018).

I wish to build the fetch clauses as follows:

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.

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 15:27:19 +01:00
adam closed this issue 2025-12-29 15:27:19 +01:00
Author
Owner

@markst commented on GitHub (Nov 27, 2018):

Here's another example:

fileprivate func fetchClauses() -> [FetchClause] {

    var fetchChainBuilder:FetchChainBuilder<Booking>

    if self.showWaitlist {
        fetchChainBuilder = From<Booking>()
            .where(\.date >= self.startDate && \.date < self.endDate)
    } else {
        fetchChainBuilder = From<Booking>()
            .where(\.date >= self.startDate && \.date < self.endDate && \.waitlist == false)
    }
    
    switch sortMode {
    case .arrivalTime:
        fetchChainBuilder = fetchChainBuilder.orderBy(.ascending(\.date))
    case .customerName:
        fetchChainBuilder = fetchChainBuilder.orderBy(.ascending("customer.name"))
    case .sections:
        fetchChainBuilder = fetchChainBuilder.orderBy(.ascending(\.tables))
        break
    }
    
    return fetchChainBuilder.fetchClauses
}
@markst commented on GitHub (Nov 27, 2018): Here's another example: ```swift fileprivate func fetchClauses() -> [FetchClause] { var fetchChainBuilder:FetchChainBuilder<Booking> if self.showWaitlist { fetchChainBuilder = From<Booking>() .where(\.date >= self.startDate && \.date < self.endDate) } else { fetchChainBuilder = From<Booking>() .where(\.date >= self.startDate && \.date < self.endDate && \.waitlist == false) } switch sortMode { case .arrivalTime: fetchChainBuilder = fetchChainBuilder.orderBy(.ascending(\.date)) case .customerName: fetchChainBuilder = fetchChainBuilder.orderBy(.ascending("customer.name")) case .sections: fetchChainBuilder = fetchChainBuilder.orderBy(.ascending(\.tables)) break } return fetchChainBuilder.fetchClauses } ```
Author
Owner

@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:

    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

    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] ```
Author
Owner

@markst commented on GitHub (Dec 6, 2018):

Thanks John!

@markst commented on GitHub (Dec 6, 2018): Thanks John!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#243