mergesort / Boutique

✨ A magical persistence library (and so much more) for state-driven iOS and Mac apps ✨
https://build.ms/boutique/docs
MIT License
899 stars 43 forks source link

i can't get items at the real time,can tell me where is wrong #58

Closed zidonJ closed 5 months ago

zidonJ commented 6 months ago

@MainActor func local() -> ReadModel? {

    let read:ReadModel? = parse.items.sfi(0)
    return read
}
zidonJ commented 6 months ago

i got it-> loadStoreTask not complete, did boutique have a real time way get the local data or offer a loadStoreTask complete handler

mergesort commented 6 months ago

Hi @zidonJ, I'm not 100% sure I understand the problem you're running into so please correct me if this isn't the question you're hoping to answer. If you need to find out whether a Store has finished loading it's items you can use the Store's .itemsHaveLoaded() function to know when the Task has completed.

The example below shows the recommended way to await a Store to see if it's loaded, after which you can update your View's state based on the Store having loaded.

struct ItemListView: View {
    @State private var itemsHaveLoaded = false

    var body: some View {
        VStack {
            AlwaysVisibleBanner()

            if self.itemsHaveLoaded {
                if self.items.isEmpty {
                    EmptyStateView()
                } else {
                    ItemView(items: self.items)
                }
            } else {
                LoadingStateView()
            }
        }
        .task({
            try await self.itemsController.items.itemsHaveLoaded()
            self.itemsHaveLoaded = true
        })
    }
}
zidonJ commented 6 months ago

thank you, i Understood. but i have an another question that why not offer a interface to get any cacheKey data ,private let storageEngine can not get it. should i keey the storageEngine.

mergesort commented 6 months ago

@zidonJ Not allowing users to access the StorageEngine from Boutique an intentional design choice. As per principle 1 & 2 discussed in #19, there is only one API to insert or remove items from Boutique. This ensures consistency between what models are in memory and what models are on disk, and means that users are always interacting at the model layer rather than trying to think of Boutique as a database that they have to architect their app around. Beyond that, the data that your CacheKeys are based on will come from your models, which means any queries you want to do in Boutique should be doable without ever needing to interface with a CacheKey.

Hope that helps answer your question!