如何通过绑定来传递取回

发布于 2025-02-03 05:17:00 字数 757 浏览 0 评论 0原文

如何将绘制绘制< cditem>作为绑定到内视图,以避免在外部更改上重新创建内部视图?

简单示例:

struct CollectionView: UIViewRepresentable {
    @Binding 
    var items: [Item] // array of structs which are core data entities representation

    ...
}

struct ItemsView: View {
    @FetchRequest(fetchRequest: CDItem.fetchReuqest)
    var items: FetchedResults<CDItem>

    var body: some View {
        CollectionView(items: /* ??? */)
    }
}

我有第一个想法 - 使用nsfetchedResultSconTroller进行一些observableObject内部,然后更新一些@published变量,nsfetchedResultscontrollerDelelerDelegate 然后使用简单的方法将其转换为绑定。但是我想使用@fetchrequest之类的糖

How to map and pass FetchedResults<CDItem> as Binding to inner view which is UIKit wrapper to avoid recreating inner view on outside changes?

Simple sample:

struct CollectionView: UIViewRepresentable {
    @Binding 
    var items: [Item] // array of structs which are core data entities representation

    ...
}

struct ItemsView: View {
    @FetchRequest(fetchRequest: CDItem.fetchReuqest)
    var items: FetchedResults<CDItem>

    var body: some View {
        CollectionView(items: /* ??? */)
    }
}

First idea I had - make some ObservableObject with NSFetchedResultsController inside and update some @Published variable with NSFetchedResultsControllerDelegate and then use easy way to convert it to Binding. But I would like to use such sugar as @FetchRequest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟稳重的好男人 2025-02-10 05:17:00

@binding根据定义,双向连接@fetchrequestnsfetchedResultScontroller不是双向。

如果您需要从商店使用项目使用

let items: [CDItem]

,或者

let items: [Item] 

您正在抽象。

影响@fetchrequestnsfetchedresultscontroller的唯一方法是对商店进行更改。他们总是在听。

@Binding is by definition a two-way connection @FetchRequest and NSFetchedResultsController are not two-way.

If you need to pass around items from the store use

let items: [CDItem]

or

let items: [Item] 

If you are abstracting.

The only way to affect @FetchRequest and NSFetchedResultsController is to make changes to the store. They are always listening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文