Fetch where relationship is contained in array #349

Closed
opened 2025-12-29 15:29:46 +01:00 by adam · 4 comments
Owner

Originally created by @TheJustikar on GitHub (Dec 16, 2020).

I am trying to fetch all entities from the class Match whose Discipline is contained in an array of disciplines.

Here is the code for fetching that is not working:

let disciplines: [Discipline] = ...
try CoreStoreDefaults.dataStack.fetchAll(From<Match>().where(Where<Match>(\.$discipline, isMemberOf: disciplines)))

The compiler error I am getting is:

Key path value type 'FieldContainer<Match>.Relationship<Discipline?>' cannot be converted to contextual type 'RelationshipContainer<Match>.ToOne<Discipline>'

Here is the Match class:

final class Match: CoreStoreObject {
    @Field .Stored("date", dynamicInitialValue: { Date() })
    var date: Date

    @Field .Stored("winnersPoints")
    var winnersPoints: Double = 0

    @Field .Stored("loosersPoints")
    var loosersPoints: Double = 0

    @Field .Relationship("discipline")
    var discipline: Discipline?

    @Field .Relationship("winners")
    var winners: [Player]

    @Field .Relationship("loosers")
    var loosers: [Player]
}

Here is the Disciple class:

final class Discipline: CoreStoreObject {
    @Field .Stored("name")
    var name: String = ""

    @Field .Relationship("matches", inverse: \.$discipline)
    var matches: [Match]
}

What exactly am I doing wrong here?

Originally created by @TheJustikar on GitHub (Dec 16, 2020). I am trying to fetch all entities from the class `Match` whose `Discipline` is contained in an array of disciplines. Here is the code for fetching that is not working: ``` let disciplines: [Discipline] = ... try CoreStoreDefaults.dataStack.fetchAll(From<Match>().where(Where<Match>(\.$discipline, isMemberOf: disciplines))) ``` The compiler error I am getting is: ``` Key path value type 'FieldContainer<Match>.Relationship<Discipline?>' cannot be converted to contextual type 'RelationshipContainer<Match>.ToOne<Discipline>' ``` Here is the Match class: ``` final class Match: CoreStoreObject { @Field .Stored("date", dynamicInitialValue: { Date() }) var date: Date @Field .Stored("winnersPoints") var winnersPoints: Double = 0 @Field .Stored("loosersPoints") var loosersPoints: Double = 0 @Field .Relationship("discipline") var discipline: Discipline? @Field .Relationship("winners") var winners: [Player] @Field .Relationship("loosers") var loosers: [Player] } ``` Here is the Disciple class: ``` final class Discipline: CoreStoreObject { @Field .Stored("name") var name: String = "" @Field .Relationship("matches", inverse: \.$discipline) var matches: [Match] } ``` What exactly am I doing wrong here?
adam added the enhancement label 2025-12-29 15:29:46 +01:00
adam closed this issue 2025-12-29 15:29:47 +01:00
Author
Owner

@TheJustikar commented on GitHub (Dec 16, 2020):

I also asked this question on stackoverflow:
https://stackoverflow.com/questions/65167443/corestore-fetch-where-relationship-is-contained-in-array

@TheJustikar commented on GitHub (Dec 16, 2020): I also asked this question on stackoverflow: https://stackoverflow.com/questions/65167443/corestore-fetch-where-relationship-is-contained-in-array
Author
Owner

@JohnEstropia commented on GitHub (Dec 16, 2020):

@TheJustikar Hi, thanks for reporting this. It looks like there's a missing overload within the Where operators.
I'll fix this in a coming update, but for now you can workaround it using String key paths:

try CoreStoreDefaults.dataStack.fetchAll(
    From<Match>()
        .where(.init(String(keyPath: \Match.$discipline), isMemberOf: disciplines))
)
@JohnEstropia commented on GitHub (Dec 16, 2020): @TheJustikar Hi, thanks for reporting this. It looks like there's a missing overload within the `Where` operators. I'll fix this in a coming update, but for now you can workaround it using `String` key paths: ```swift try CoreStoreDefaults.dataStack.fetchAll( From<Match>() .where(.init(String(keyPath: \Match.$discipline), isMemberOf: disciplines)) ) ```
Author
Owner

@JohnEstropia commented on GitHub (Dec 16, 2020):

You can also try the "matches" operator:

try CoreStoreDefaults.dataStack.fetchAll(
    From<Match>()
        .where(disciplines ~= \.$discipline)
)
@JohnEstropia commented on GitHub (Dec 16, 2020): You can also try the "matches" operator: ```swift try CoreStoreDefaults.dataStack.fetchAll( From<Match>() .where(disciplines ~= \.$discipline) ) ```
Author
Owner

@Tykhonkov commented on GitHub (Dec 5, 2022):

would be great to add this example in the documentation

@Tykhonkov commented on GitHub (Dec 5, 2022): would be great to add this example in the documentation
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#349