CoreStore icon indicating copy to clipboard operation
CoreStore copied to clipboard

SwiftUI : how to use ListPublisher in Views

Open distantnative opened this issue 5 years ago • 1 comments

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.

distantnative avatar Nov 09 '20 11:11 distantnative

@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 avatar Nov 11 '20 03:11 JohnEstropia