Replies: 3 comments
-
I've wrestled with the same question when using the library for database observation. Fetching collections of items is ok because an easy default is an empty collection. Fetching a single item is tougher, though, since you don't have an identifier to use as the default. I've been using If you're using that shared value in a bunch of places, maybe you could extract a non-optional Shared value in a parent feature and pass it to child features? It means injecting the value into the children which isn't convenient, but it would save you the effort of unwrapping if that's something you have to do a lot. |
Beta Was this translation helpful? Give feedback.
-
Hi @cabeca, to summarize your use case, you have a list of products, the user taps one to bring up a sheet, and from that point forward you want to have some kind of In that case it seems quite easy to avoid an optional. You would first design a @Observable class ProductDetailModel { // Could also be a View
@SharedReader var product: Product
// ...
} Notice that we are not using the key directly with So, you can add an initializer to init(product: Product) {
_product = SharedReader(wrappedValue: product, .product(id: product.id))
// ...
} Now the And then in the list feature you can construct this model by passing along the product: func tapped(product: Product) {
let model = ProductDetailModel(product: product)
// ...
} That's the basics. |
Beta Was this translation helpful? Give feedback.
-
Thank you both for your responses. I now have a better understanding of how to use these tools. |
Beta Was this translation helpful? Give feedback.
-
Hello,
While considering the use of this library, I always get lost in a particular use case.
Let's say that I have an API client for dealing with a certain product the user might have. There are endpoints for listing the Products the client has, as well as endpoints to get a single product via
ProductId
and subscriptions for receiving updates to that single product. Let's say that I'm using the@Dependency
library to have a completeProductClient
struct with all these endpoints.Now, the user can choose a product from a list on screen, and if he does, a sheet is presented with a detail view of the product. This view might be super complex and be composed of many other views, but it is crucial that all views are kept up to date with the latest Product state. Sub views can also call api endpoints to change the state of the product. Pretty standard stuff.
My question here is: what would be the best use of
@Shared
here for the optional currently selected product? Would it be:@Shared(.inMemory("currentProduct")) var currentProduct: Product? = nil
@Shared(.product(productId)) var product: Product = ????
If using an optional currentProduct, you have to handle optionals everywhere, in a way that is avoided in traditional approaches, if you require non optionals to initialize your models.
If you define your own
.product
"persistence strategy" to use the api endpoints to get product by Id and use subscriptions to keep it up to date, how do you pass the productId into the@Shared
declaration? Does it mean the init dance to assign to _product everywhere? And what do we put as the default value in this case?Any pointers in the right direction would be much appreciated.
Beta Was this translation helpful? Give feedback.
All reactions