where clause for one to many relationship #271

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

Originally created by @seanliu1 on GitHub (May 21, 2019).

I am looking for something similar to [NSPredicate predicateWithFormat:@"person.children.@count > 0"]

I tried to use
dataStack.fetchAll(From().where(\person.children.count == 0)

but it will give error
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.
I wonder what is the correct syntax for this case?

Originally created by @seanliu1 on GitHub (May 21, 2019). I am looking for something similar to [NSPredicate predicateWithFormat:@"person.children.@count > 0"] I tried to use dataStack.fetchAll(From<person>().where(\person.children.count == 0) but it will give error Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value. I wonder what is the correct syntax for this case?
adam added the question label 2025-12-29 15:28:02 +01:00
adam closed this issue 2025-12-29 15:28:02 +01:00
Author
Owner

@JohnEstropia commented on GitHub (May 23, 2019):

Sorry I haven't gotten to add this anywhere in the README yet, but this is the current syntax for it:

dataStack.fetchAll(
    From().where((\.person ~ \.children).count() == 0)
)
@JohnEstropia commented on GitHub (May 23, 2019): Sorry I haven't gotten to add this anywhere in the README yet, but this is the current syntax for it: ```swift dataStack.fetchAll( From().where((\.person ~ \.children).count() == 0) ) ```
Author
Owner

@seanliu1 commented on GitHub (May 23, 2019):

Thanks, I actually already figure out but not as elegant as yours.

I was using where(format: "episodes.@count == 0", argumentArray: nil) it also works, but it wont do any compile time check for your.

@seanliu1 commented on GitHub (May 23, 2019): Thanks, I actually already figure out but not as elegant as yours. I was using where(format: "episodes.@count == 0", argumentArray: nil) it also works, but it wont do any compile time check for your.
Author
Owner

@Vertuals commented on GitHub (Nov 5, 2021):

@JohnEstropia What if we want a person from the list with a particular child id ?

Sorry I haven't gotten to add this anywhere in the README yet, but this is the current syntax for it:

dataStack.fetchAll(
    From().where((\.person ~ \.children).count() == 0)
)

what if we want a child from the list with a particular id ?

@Vertuals commented on GitHub (Nov 5, 2021): @JohnEstropia What if we want a person from the list with a particular child id ? > Sorry I haven't gotten to add this anywhere in the README yet, but this is the current syntax for it: > > ```swift > dataStack.fetchAll( > From().where((\.person ~ \.children).count() == 0) > ) > ``` what if we want a child from the list with a particular id ?
Author
Owner

@JohnEstropia commented on GitHub (Nov 8, 2021):

@Vertuals Sorry, there are no syntax sugars for them in CoreStore yet, but you can use the ANY and ALL operators in your own NSPredicate and pass it to the CoreStore where() clause

@JohnEstropia commented on GitHub (Nov 8, 2021): @Vertuals Sorry, there are no syntax sugars for them in CoreStore yet, but you can use the [`ANY` and `ALL`](https://stackoverflow.com/questions/30076452/difference-between-all-any-and-some-operators-in-nspredicate) operators in your own `NSPredicate` and pass it to the CoreStore `where()` clause
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#271