I have a model view that has fetching contacts function:
class ContactsStore: ObservableObject {
@Published var contacts = [CNContact]()
func fetch() {} ...
And then in my View:
@EnvironmentObject var store: ContactsStore
var groupedContacts: [String: [CNContact]] {
.init (
grouping: store.contacts,
by: {$0.nameFirstLetter}
)
}
...
List() {
ForEach(self.groupedContacts.keys.sorted(), id: \.self) { key in ...
I've shortened my code for the convenience, will add/edit if needed. The issue I faced - every time my View is rendered the fetch
function is called and my array of contacts is duplicated in my view. TIA for a help
UPD: Duplication happens due to method calling fetch
in the List .onAppear
. So I'm looking at how to call this method only once and not every time the View appears.