I am populating a List in a SwiftUI app by making a call to the ViewModel file and passing the current user in the onAppear(). This filters results pulled from a database to only show results related to the user. Everything below has worked well until I installed iOS 15 on device. The code works as expected in the iOS Simulator in iOS 14, iOS 15, and on device with iOS 14. The issue occurs with iOS 15 on device. The message printed is below.
ForEach<Array, String, NavigationLink<OfferRowView, ModifiedContent<OfferDetailView, _EnvironmentKeyWritingModifier>>>: the ID occurs multiple times within the collection, this will give undefined results!
The view does not load the items in the list. I've printed to test to see if they are duplicated and they are not.
View File
struct OfferHistoryView: View {
let db = Firestore.firestore()
@EnvironmentObject var authSession: AuthSession
@EnvironmentObject var offerHistoryViewModel: OfferHistoryViewModel
var body: some View {
return VStack {
List {
ForEach(self.offerHistoryViewModel.offerRowViewModels, id: \.id) { offerRowViewModel in
NavigationLink(destination: OfferDetailView(offerDetailViewModel: OfferDetailViewModel(offer: offerRowViewModel.offer, listing: offerRowViewModel.listing ?? testListing1))
.environmentObject(authSession)
) {
OfferRowView(offerRowViewModel: offerRowViewModel)
}
} // ForEach
} // List
.navigationBarTitle("Offer History")
} // VStack
.onAppear(perform: {
self.offerHistoryViewModel.startCombine(currentUserUid: self.authSession.currentUserUid)
})
} // View
}
View Model File
class OfferHistoryViewModel: ObservableObject {
var offerRepository: OfferRepository
// Published Properties
@Published var offerRowViewModels = [OfferRowViewModel]()
// Combine Cancellable
private var cancellables = Set<AnyCancellable>()
// Intitalizer
init(offerRepository: OfferRepository) {
self.offerRepository = offerRepository
}
// Starting Combine - Filter results for offers created by the current user only.
func startCombine(currentUserUid: String) {
offerRepository
.$offers
.receive(on: RunLoop.main)
.map { offers in
offers
.filter { offer in
(currentUserUid != "" ? offer.userId == currentUserUid : false)
}
.map { offer in
OfferRowViewModel(offer: offer, listingRepository: ListingRepository())
}
}
.assign(to: \.offerRowViewModels, on: self)
.store(in: &cancellables)
}
}
Any help would be greatly appreciated.