TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

ScrollView with large data set from Core Data

Forums > SwiftUI

I now have a view where I need to display thousands of results from Core Data and I need to figure out how to handle the delay; either by optimizing the code, adding a ProgressView() or some other option. What is the best way to let the user know something is loading, or better yet, avoid the delay?

When I click on a button, there is an obvious and unwanted delay before it pushes to the new view displaying all the data. I've tried LazyVStack, but since I'm loading by sections, the view jumps around as data is loaded as the user scrolls.

I thought maybe using a technique similar to loading infinite lists, but can't figure out how that would work with a Core Data fetch and the sections/foreaches. I also thought maybe using a ProgressView() but I can't figure out how to know that the results from the Core Data fetch have fully loaded.

Code snippet below the example (sorry if I missed a } or a )). I have categories of grouped items and want the view to look something like this:

Category 1
Group 1-1
Item 1-1-1
...
Item 1-1-n

Group 1-2
Item 1-2-1
...
Item 1-2-n

Category 2
Group 2-1
Item 2-1-1

and so on

BigView

var body: some View {
            ScrollView{
            //categoryList is a [String] of Category Names and each Group has an attribute called CategoryName
              ForEach(categoryList, id:\.self){category in
                  Section(header:
                            Text(category)
                  ){
                    VStack{
                    //Group is an entity in Core Data, and the fetchedEntity with a predicate on CategoryName
                      ForEach(groups){group in
                          BigSubView(group: Group)
                        }
                      }
                    }
                  }
              }

BigSubView

@ObservedObject var group: Group

 var body: some View {
        Section(header:
                HStack{
                  Text(group.wrappedName)
                }.background(Color(UIColor.systemFill))){

        VStack{
        //A Group has a one-many relationship with Item entity in Core Data, I convert that to an [Item]
          ForEach(group.itemsArray, id:\.self){item in
              Text(item.wrappedName)
        }
      }
    }
  }

4      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.