Creating a list monitor from a relationship #283

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

Originally created by @zachwaugh on GitHub (Aug 8, 2019).

I'm currently creating a list monitor by building a query, but I'm wondering if there is a simpler way. For example, if I have a Post entity which has many Authors (and Author has many Posts), I need to do something like this to get a live list of all the posts from an author:

let author: Author = // assume I have this from another query
let monitor = CoreStore.monitorList(
    From<Post>()
        .where(format: "authors CONTAINS %@", author)
        .orderBy(.descending(\.createdAt))
 )

But would I love to do this:

let monitor = CoreStore.monitorList(author.posts)

Is there any way to do something along those lines instead of recreating the full query?

Originally created by @zachwaugh on GitHub (Aug 8, 2019). I'm currently creating a list monitor by building a query, but I'm wondering if there is a simpler way. For example, if I have a `Post` entity which has many `Author`s (and `Author` has many `Posts`), I need to do something like this to get a live list of all the posts from an author: ```swift let author: Author = // assume I have this from another query let monitor = CoreStore.monitorList( From<Post>() .where(format: "authors CONTAINS %@", author) .orderBy(.descending(\.createdAt)) ) ``` But would I love to do this: ```swift let monitor = CoreStore.monitorList(author.posts) ``` Is there any way to do something along those lines instead of recreating the full query?
adam closed this issue 2025-12-29 15:28:10 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Aug 13, 2019):

NSFetchResultsController, which ListMonitor uses internally, does not support observing on relationships. You might want to create a denormalized entity between Post and Author that will contain one row for each author for a particular post, observe that list, extract an array of objectIDs on didChange, and fetch the list of Posts using the objectIDs.

@JohnEstropia commented on GitHub (Aug 13, 2019): `NSFetchResultsController`, which `ListMonitor` uses internally, does not support observing on relationships. You might want to create a denormalized entity between `Post` and `Author` that will contain one row for each author for a particular post, observe that list, extract an array of objectIDs on didChange, and fetch the list of `Post`s using the objectIDs.
Author
Owner

@zachwaugh commented on GitHub (Aug 13, 2019):

Thanks for the answer! It's not too bad to recreate the query, just hoping to avoid the redundancy if possible.

@zachwaugh commented on GitHub (Aug 13, 2019): Thanks for the answer! It's not too bad to recreate the query, just hoping to avoid the redundancy if possible.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#283