9

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?

1
  • Since there was no comment, I can only assume that my answer below was not appropriate for some reason, so I deleted it. If you are still looking for some hints and tips on how to use CoreData with SwiftUI and need a place to start, check out my answer to stackoverflow.com/questions/57348127/… Commented Aug 21, 2019 at 23:41

2 Answers 2

6

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
}

Sign up to request clarification or add additional context in comments.

1 Comment

Hello Ugo, I dont't get how the code you postet helps ? What are the next steps ?
0

I think that's the reason of using ForEach() for loops in SwiftUI, so the view can control how many elements needs to instantiate to fill the screen.

And the Array from a Core Data fetch request probably doesn't contains all the objects in memory, they only get instantiated when they are accessed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.