Hello everyone. I have a small question about Weak Self. In the example below, I am doing a long process to the data I query with SwiftData. (For this reason, I do it in the background.) I don't know if there is a possibility of a memory leak when the view is closed because this process takes a long time.
import SwiftUI
import SwiftData
struct ActiveRegView: View {
@Query(filter: #Predicate<Registration> {
$0.activeRegistration
}, animation: .default) private var regs: [Registration]
@Environment(ViewModel.self) private var vm
@State private var totalParkingFee: Decimal = 0
var body: some View {
...
.onAppear {
var totalParkingFee = Decimal()
DispatchQueue.global(qos: .userInitiated).async {
for reg in regs {
totalParkingFee += vm.parkingFee(from: reg.entryRegistration.entryDate, to: .now, category: reg.category, isCustomPrice: reg.isCustomPrice)
}
DispatchQueue.main.async {
withAnimation {
totalParkingFee = totalParkingFee
}
}
}
}
}
}
I use ViewModel
with @Environment
, so I don't initilize ViewModel every time view is initilized. I know it's a simple question and I thank you in advance for your answers.