I am making an app with SwiftUI and UIkit, I exploit UIkit for the primary app controller and navigation, and I exploit SwiftUI for app design.
The app works very nicely, however I am apprehensive concerning the reminiscence leaks. It’s because the ViewModels I exploit to go information between views do not name desinit whene the view disappears. I do know that in SwiftUI views usually are not disposed instantly, however since I am utilizing UIKit to navigate I do not know what the issue is.
//The ViewModel for every consumer fetched
inside class UserViewModel: ObservableObject, Identifiable {
//MARK: - Propeties var currentListener: ListenerRegistration?
@Printed var request: Request?
@Printed var consumer: Consumer
init(consumer: Consumer) {
self.consumer = consumer
getRequest()
fetchAdmins()
}
deinit {
//Dosnt get referred to as removeListener()
}
func getRequest() {
guard let uid = Auth.auth().currentUser?.uid else {return}
guard let id = id else {return}
self.currentListener = Collections.requests(id).doc(uid).addSnapshotListener { snapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
if ((snapshot?.exists) != nil) {
if let request = attempt? snapshot!.information(as: Request.self) {
DispatchQueue.principal.async {
self.request = request
}
}
}
}
}
func removeListener() {
self.currentListener?.take away()
}
}
}
//The ViewModel to fetch all of the customers ViewModels
class UsersViewModel: ObservableObject {
@Printed var customers = [UserViewModel]()
func fetch() {
DispatchQueue.international(qos: .background).async {
Collections.customers.getDocuments(completion: { snapshot, err in
guard let paperwork = snapshot?.paperwork else { return } let customers = paperwork.compactMap({ attempt? $0.information(as: Consumer.self) })
customers.forEach { consumer in
let vm = UserViewModel(consumer: consumer)
DispatchQueue.principal.asyncAfter(deadline: .now() + 1) {
self.customers.append(vm)
}
}
})
}
} }
//Present the customers cells with the ViewModel
struct HomeView: View {
@ObservedObject var usersViewModels: UsersViewModel
//MARK: - Init
init() {
self.usersViewModels = UsersViewModel()
}
var physique: some View {
ListView(content material: {
ForEach(usersViewModels) { usersViewModel in
UserCell(viewModel: usersViewModel).id(consumer.id)
}
})
}
}
That is how I navigate between controllers and views of my app. I do not use NavigationLinks:
public static func push<Content material: View>(view: Content material) {
DispatchQueue.principal.async {
guard let tabBarController = UIApplication.rootViewController as? UITabBarController, let navigationController = tabBarController.selectedViewController as? UINavigationController else { return nil }
if let navigationController = UIApplication.getCurrentNavigationController() {
navigationController.pushViewController(HostingController(content material: view), animated: true)
}
}
}
Does anybody know if this technique that I’m utilizing to navigate may cause me reminiscence issues? And you recognize why my app does not cut back its reminiscence each time I shut a window, it simply will increase an increasing number of.