Search code examples
iosswiftcore-datacorestore

CoreStore: Fetch where relationship is contained in array


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?


Solution

  • I also answered your question over at the CoreStore issues section.

    There was a missing overload in the Where initializers which I'll add in an upcoming update, but you can also use the ~= operator as a shorthand syntax:

    try CoreStoreDefaults.dataStack.fetchAll(
        From<Match>()
            .where(disciplines ~= \.$discipline)
    )