SwiftUI List with Core Data: memory usage while scrolling

24 views Asked by At

I want to display a list of 'contacts' in my app. Currently, I use UIKit currently, and with UITableView's cell reuse, even with 5000+ rows, the memory usage is great ... it loads at about 80MB and stays around that no matter how much I scroll up or down the list.

I implemented the same list with SwiftUI List, and the memory usage while scrolling is very different ... the initial load is about the same, but each time I go down the whole list, it adds 20-30MB to the memory usage (according to Xcode).

Is this a side-effect of using SwiftUI's List, or am I doing something wrong here?

The List and it's row views are very simple in this example:

struct CJContactsListView: View {
        
    @SectionedFetchRequest var sectionContacts: SectionedFetchResults<String, Person>
    
    init() {
    
        let fetchRequest = Person.allContactsFetchRequest()
        _sectionContacts = SectionedFetchRequest(fetchRequest: fetchRequest, sectionIdentifier: \.normalizedSectionLetter!, animation: .default)
    }

    var body: some View {
        
        List {
            ForEach(sectionContacts) { section in
                Section(header: Text(section.id)) {
                    
                    ForEach(section) { person in
                        
                        CJContactsListLabelRowView(person: person)
                    }
                }
            }
        }
        .listStyle(.plain)
    }
}


struct CJContactsListLabelRowView: View {
    @ObservedObject var person: Person
    
    var body: some View {
        HStack (alignment: .center, spacing: 8) {
            
            VStack (alignment: .leading){
                if let displayName = person.displayName {
                    Text(displayName).font(.headline)
                }
                if let companyName = person.companyName {
                    Text(companyName).font(.subheadline).foregroundColor(.secondary)
                }
            }
        }
    }
}

And the fetch request setup:

extension Person: Identifiable {
    public var id: String {
        return self.objectID.uriRepresentation().absoluteString
    }
    
    public static func allContactsFetchRequest() -> NSFetchRequest<Person> {
        let request = Person.fetchRequest()
        request.sortDescriptors = Person.makeSortDescriptors()
        request.predicate = NSPredicate(format: "(isContactArchived == nil || isContactArchived == 0)")
        request.fetchBatchSize = 100
        request.relationshipKeyPathsForPrefetching = ["tags"]
        return request
    }
}

Here's a snapshot of the Xcode debugging view showing the memory profile growing:

enter image description here

0

There are 0 answers