CoreStore sectioned list monitor how to specify .where clause in runtime

Published

I have this code in my init method:

self.monitor = CoreStore.monitorSectionedList(
        From<ListEntityType>()
            .sectionBy(#keyPath(ListEntityType.muscle.name)) { (sectionName) -> String? in
                return "(String(describing: sectionName)) years old"
            }
            .orderBy(.ascending(#keyPath(ListEntityType.muscle.name)), .ascending(.name))
    )

I want to somehow add to this monitor .where condition in runtime.

ListEntityType is a type alias of an entity called Exercise. So each Exercise contains one to one relationship Muscle (exercise.muscle).

each entity has unique identifier attribute.

I have array of muscles identifier which I want to use as filter to display object from sectioned list.

How can loop through this identifiers to add then to .where clause to the CoreStore.monitorSectionedList

I probably expect something like this but not 100% sure just assumption:

let predicate = NSPredicate(format: “%K = $ARGUMENT”)

    var predicates = [NSPredicate]()

    if let muscles : [MuscleGroupEntity] = workout?.muscles.toArray() {
        for muscle in muscles
        {
            let myNewPredicate = predicate.withSubstitutionVariables(["ARGUMENT" : muscle.id])
            predicates.append(myNewPredicate)
        }
    }

    self.monitor = CoreStore.monitorSectionedList(
        From<ListEntityType>()
            .sectionBy(#keyPath(ListEntityType.muscle.name)) { (sectionName) -> String? in
                return "(String(describing: sectionName)) years old"
            }
            .where(format:"%K = %@", argumentArray: predicates)
            .orderBy(.ascending(#keyPath(ListEntityType.muscle.name)), .ascending(.name))
    )

this code crashed with error:

-[NSComparisonPredicate rangeOfString:]: unrecognized selector sent to

I guess this is something from predicate I found here, but not sure how to use it correctly in my case

EDITED For MartinM comments:

I have ExerciseEntity with a property Muscle.

If I want to get all exercises using just one muscle id it pretty simple:

.where(format:"%K = %@", #keyPath(ExerciseEntity.muscle.id), "id_12345")

But I want to specify more muscles and define them in runtime. Somehow I need to understand what the format is and what the arguments and how to pass array of identifiers instead just one "id_12345"

Source: Ios

Published
Categorised as core-data, corestore, ios, nspredicate, swift

Answers

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq