Skip to content

How to limit the number of fetched records in the context of an HasMany association? #1735

Answered by groue
adilrc asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @adilrc,

Is there a way to limit the number of fetched books?

Yes, but not trivially, because this is not a built-in GRDB feature.

You'll need to do by hand what including(all:) does for you.

The easiest technique is to perform as many book requests as there are authors:

extension Author {
    static let books = hasMany(Book.self)
}

struct AuthorInfo: Decodable, FetchableRecord {
    struct PartialBook: Decodable, FetchableRecord {
        var title: String
        var year: Int
    }
    var author: Author       // The base record
    var books: [PartialBook] // A collection of partial associated records
}

// Fetch authors (filtered, ordered, whatever)
let authors = try Author.f…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@adilrc
Comment options

@groue
Comment options

Answer selected by adilrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants