-
-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SwiftUI @Published ViewModel example #477
Comments
Update: itemsSnapshot.compactMap { $0.object } Not mentioned in the docs, so not sure if this is a good tactic. |
Hi, have you checked the CoreStoreDemo project? There are examples on how to use |
Yes, I did thank you. I also looked at the unit tests. Like I said in my intro, I'd rather not have the View have a dependency on the CoreStore library, but the view model is fine. In my experience, in more complex enterprise use cases, one rarely goes from a Core Data object straight to View. A ViewModel or Interactor grooms, processes, filters, combines, splices the core data before forming a viewModel object. Additionally, the listPublisher.reactive
.snapshot(emitInitialValue: true)
.flatMap { // or compactMap { // or map {
... // more grooming
.sink... I went straight to this section and thus was confused as to what the |
The API provides the necessary endpoints for your app. If you prefer not to depend on CoreStore, then you would have to write that layer yourself.
|
Ah, I guess you are using |
Appreciate the comments John, I do like what CoreStore has achieved. I think the library is not addressing a common architectural pattern, where a To give a crude example, let's say you have a Whatsapp style app. Core Data would store the encrypted So how does one achieve this? Something like: class ViewModel: ObservableObject {
@Published var messageViewModels: [MessageViewModel] = []
init() {
let listPublisher = dataStack.publishList(
From<Message>()
.orderBy(.ascending(\.timestamp))
)
listPublisher.reactive
.snapshot(emitInitialValue: true)
.receive(on: RunLoop.main)
.flatMap { snapshot in
// convert snapshot into MessageViewModel array
}
.sink(receiveCompletion: { completion in
...
}, receiveValue: { [weak self] messageViewModels in
self?.messageViewModels = messageViewModels
})
.store(in: &cancellables)
}
} |
I understand that there are architectures where you have to provide the per-object ViewModels directly, and in fact we do use similar cases in our projects. The problem is that SwiftUI kind of forces us to wrap our ViewModels in some sort of Now of course you are free to limit the dependency to CoreStore and implement this yourself. In that case, I would still recommend you check how @ListState and @ObjectState does this internally and base your implementation on that. (Or alternatively, ListReader and ObjectReader depending on your requirements) |
I'm a little confused as to how to use this lib in a ViewModel SwiftUI context. I'd rather not have the View have a dependency on the CoreStore lib.
I'm not sure what to do with the snapshot and how to convert it to the @published array. Will this array then be diffable?
I'm more likely to map the objects to a detailedViewModel object. Something like:
I'm fundamentally not understanding how to get access to the Core Data objects from the listSnapshot.
The text was updated successfully, but these errors were encountered: