Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SwiftUI with Core Data

SwiftUI tables require a binding to an array storing the entirety of your model objects in memory. For small datasets, the tradeoff of convenience for performance makes sense. But for datasets with tens/hundreds of thousands of values, the old-school approach to rendering tables through queries to a datasource still seems like the way to go. (Consider a simple dictionary/thesaurus app.).

Is there a way to implement dataSource-style/CoreData-backed tables within SwiftUI?

like image 337
pseudosudo Avatar asked Jun 14 '19 01:06

pseudosudo


1 Answers

List does not require an Array. The Data must conform to the RandomAccessCollection protocol. This could also be your 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
}

like image 187
Ugo Arangino Avatar answered Oct 14 '22 21:10

Ugo Arangino