Skip to content

Using GRDB with protocols #1049

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

You must be logged in to vote

Hello @hell001,

It is not possible to write MyProtocol.fetchAll(db), as the compiler says. You have to tell which concrete type (struct, class or enum) should be decoded. And you'll fetch an homogeneous array, that only contains values of this type.

You want to fetch values of multiple types, so we have no choice: we have to look for another fetching technique.

protocol MyProtocol { ... }
struct A: MyProtocol { ... }
struct B: MyProtocol { ... }

// How to fetch an array that contains some A and some B?
let myRecords: [MyProtocol] = ...

Some database rows encode an A, and some other encode a B. Let's suppose the type of each encoded value is stored in a column kind whose value is "A", or "B"

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by groue
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