SwiftUI : how to use ListPublisher in Views #347

Open
opened 2025-12-29 15:29:40 +01:00 by adam · 1 comment
Owner

Originally created by @distantnative on GitHub (Nov 9, 2020).

Hi there,

I'm quite excited about finding CoreStore but I am struggling a bit to put it in SwiftUI - which might be just my poor skills.

So I have an object

class Segment: CoreStoreObject {
    @Field.Stored("purpose")
    var purpose: String = ""
    
    static public func upcoming() -> ListPublisher<Segment> {
        DataStore.stack.publishList(
            From<Segment>()
                .where(\.$from >= Date())
                .orderBy(.ascending(\.$from))
        )
    }
}

and then my view where I want to list them

struct SegmentsListView: View {
    
    @ObservedObject var upcoming: ListPublisher<Segment> = Segment.upcoming()
    
    var body: some View {
        List {
            SegmentsListSectionView(
                title: "Upcoming",
                segments: self.upcoming
            )
        }
  }
}

How can I change this, when SegmentsListSectionView excepts segments to be of type [Segment]?
I found that I can avoid errors with [ListSnapshot<Segment>.Element] but this seems like I am just diving into trouble.

And should I even go this route? The docs say ListPublisher does not track detailed inserts, deletes, and moves - so deleting an item from my list won't work with ListPublisher - but ListMonitor doesn't support SwiftUI.

Would appreciate any help - even if the answer is that it won't work with SwiftUI and I need to go back to pure CoreData (which I have loads of trouble as well - so my hope has been with CoreStore).

Thank you.

Originally created by @distantnative on GitHub (Nov 9, 2020). Hi there, I'm quite excited about finding CoreStore but I am struggling a bit to put it in SwiftUI - which might be just my poor skills. So I have an object ```swift class Segment: CoreStoreObject { @Field.Stored("purpose") var purpose: String = "" static public func upcoming() -> ListPublisher<Segment> { DataStore.stack.publishList( From<Segment>() .where(\.$from >= Date()) .orderBy(.ascending(\.$from)) ) } } ``` and then my view where I want to list them ```swift struct SegmentsListView: View { @ObservedObject var upcoming: ListPublisher<Segment> = Segment.upcoming() var body: some View { List { SegmentsListSectionView( title: "Upcoming", segments: self.upcoming ) } } } ``` How can I change this, when `SegmentsListSectionView` excepts `segments` to be of type `[Segment]`? I found that I can avoid errors with `[ListSnapshot<Segment>.Element]` but this seems like I am just diving into trouble. And should I even go this route? The docs say `ListPublisher does not track detailed inserts, deletes, and moves` - so deleting an item from my list won't work with `ListPublisher` - but `ListMonitor` doesn't support SwiftUI. Would appreciate any help - even if the answer is that it won't work with SwiftUI and I need to go back to pure CoreData (which I have loads of trouble as well - so my hope has been with CoreStore). Thank you.
adam added the question label 2025-12-29 15:29:40 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Nov 11, 2020):

@distantnative ListPublisher will work for your needs here. I admit the documentation may be misleading here:

ListPublisher does not track detailed inserts, deletes, and moves

It just means that you'll get the updated list of objects, but you won't know which objects were inserted, deleted, edited, or moved.
Since you are using SwiftUI, I assume that your Views just iterate on the ListSnapshot whenever the ListPublisher is updated, and if that's the case, it should be suitable for your use case.

@JohnEstropia commented on GitHub (Nov 11, 2020): @distantnative `ListPublisher` will work for your needs here. I admit the documentation may be misleading here: > `ListPublisher` does not track detailed inserts, deletes, and moves It just means that you'll get the updated list of objects, but you won't know which objects were inserted, deleted, edited, or moved. Since you are using SwiftUI, I assume that your `View`s just iterate on the `ListSnapshot` whenever the `ListPublisher` is updated, and if that's the case, it should be suitable for your use case.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#347