将SwiftUI与核心数据结合使用

gar*_*ois 9 core-data swiftui

SwiftUI表需要绑定到将整个模型对象存储在内存中的数组。对于小型数据集,权衡性能的便利性是有意义的。但是对于具有成千上万个值的数据集,通过查询数据源来呈现表的老式方法似乎仍然是可行的方法。(考虑一个简单的字典/同义词库应用程序。)。

有没有一种方法可以在SwiftUI中实现由数据源样式/ CoreData支持的表?

Ugo*_*ino 6

列表不需要Array。的Data必须符合RandomAccessCollection协议。这也可能是您的NSFetchedResultsController

extension List {
    /// Creates a List that computes its rows on demand from an underlying
    /// collection of identified data.
    @available(watchOS, unavailable)
    public init<Data, RowContent>(
        _: Data,
        selection _: Binding<Selection>?,
        rowContent _: @escaping (Data.Element.IdentifiedValue) -> RowContent
    ) where Content == ForEach<Data, HStack<RowContent>>,
        Data: RandomAccessCollection,
        RowContent: View,
        Data.Element:
        Identifiable

    /// Creates a List that computes its rows on demand from an underlying
    /// collection of identified data.
    @available(watchOS, unavailable)
    public init<Data, RowContent>(
        _: Data,
        selection _: Binding<Selection>?,
        action _: @escaping (Data.Element.IdentifiedValue) -> Void,
        rowContent _: @escaping (Data.Element.IdentifiedValue) -> RowContent
    ) where Content == ForEach<Data, Button<HStack<RowContent>>>,
        Data: RandomAccessCollection,
        RowContent: View, Dat
}

Run Code Online (Sandbox Code Playgroud)