How to get total records in SwiftData so can display the proper records or ContentUnavailableView (Custom or .search)

Watched Paul Hudson long SwiftData Tutorial and wanted to modify it by putting in the built-in ContentUnavailableView.search “No Search Results” view and also a custom “ContentUnavailableView” when there are no physical records/objects in SwiftData Person class to start. This custom View has an “add” button.

Got it working by using a computed property private var allPeople: Int that gives me the count and then if-else to make the decision. Is there another or better way of grabbing this “Person” count and is this the proper way to branch based on this count using .onAppear? I have seen .overlay used but wasn’t sure which was the proper design method.

Whatever I use to keep track of the count in ContentView, I need to send into PeopleView so I can update the count if I delete someone in there.

Still learning and thanks,

Scott

struct ContentView: View {
    @Environment(.modelContext) var modelContext
    
    @State private var destPath = NavigationPath()
    @State private var sortOrder = [SortDescriptor(Person.name)]
    @State private var searchText = ""
    
    // Current "Person" count
    @State private var personCount = 0
    
    // TEST - Finding the total object count for Person and see if I could keep it updated in this view
    private var allPeople: Int {
        let decriptor = FetchDescriptor<Person>(predicate: #Predicate { !$0.name.isEmpty })
        return (try? modelContext.fetchCount(decriptor)) ?? 0
    }
        
    var body: some View {
        NavigationStack(path: $destPath) {
            Group {
                if personCount > 0 {
                    PeopleView(searchString: searchText, weHavePeople: $personCount, sortOrder: sortOrder)
                        .navigationTitle("Face Facts Man: (personCount)")
                        .toolbar {
                            Menu("Sort", systemImage: "arrow.up.arrow.down") {
                                Picker("Sort", selection: $sortOrder) {
                                    "
                                    "
                                }
                            }
                            
                            Button("Add Person", systemImage: "plus", action: addPerson)
                        }
                        .searchable(text: $searchText, prompt: "Find Some People...")   
                } else {
                    ContentUnavailableView(label: {
                        Label("You Must Know Someone", systemImage: "list.bullet.rectangle.portrait")
                    }, description: {
                        Text("ContentView: Go ahead, add some people! (personCount)")
                    }, actions: {
                        Button("Add A Person", systemImage: "person.badge.plus", action: addPerson)
                            .buttonStyle(.borderedProminent)
                    })
                }
            }
            .navigationDestination(for: Person.self) { chosenPerson in
                EditPersonView(person: chosenPerson, navPath: $destPath)
            }
            // TEST: Put in to see if the count was updating and assign so that we can branch above in Group
            .onAppear {
                personCount = allPeople
            }
        }
    }
    
    /*
     Creates the new person and inserts it as "blank" into context.  Then change the destPath
     to the correct Person we are going to edit.  It's now binded and auto-saving is now done as
     fields are changed on edit screen.
    */
    func addPerson() {
        let newPerson = Person(name: "", emailAddress: "", details: "")
        modelContext.insert(newPerson)
        destPath.append(newPerson)
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật