Updated 30 April 2021
In this Blog , we are going to learn about the deinitializer in Swift. Deinitializer is called in the Program to deallocate the memory space before a class instance deallocated from the Program. In Swift deinitializer are used to remove the Object which are not clean or removed by the ARC (Automatic Reference Counter).
So , In Order to Clean those reference we use deinitializer in Swift.
Deinitializer are called when the instance of a class is destroyed. For example the class car instance is created and its initialiser is called as soon as the instance is destroyed , init is called, which gives the advantage of house keeping after the destroy of the instanced the class.
1 2 3 4 5 6 7 8 9 10 11 |
class Car { var name = "Maruti" init() { print("New Maruti is created") } deinit { print("Maruti is no more") } } |
For reference let take the example of the listener in the Firestore for listening for the changes in the data at run time.
1 |
import FirebaseFirestore |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
class PeopleVC: UIViewController { //MARK:- @IBOutlet weak var tableView: UITableView! var peoples = [People]() var category: Category! var listener: ListenerRegistration! var db: Firestore! var locationManager: CLLocationManager! //let locationManager = CLLocationManager() //MARK:- viewLifecycleMethods override func viewDidLoad() { super.viewDidLoad() db = Firestore.firestore() setupTableView() // checkLocationServices() } override func viewWillAppear(_ animated: Bool) { fetchCollection() //setPeoples() } override func viewWillDisappear(_ animated: Bool) { listener.remove() } fileprivate func setupTableView() { tableView.dataSource = self tableView.delegate = self tableView.register(UINib(nibName: Identifiers.PeopleCell, bundle: nil), forCellReuseIdentifier: Identifiers.PeopleCell) tableView.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 200 } } //MARK:- UITableViewDataSource and UITableViewDelegate extension PeopleVC: UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return peoples.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.PeopleCell, for: indexPath) as? PeopleCell { cell.configureCell(people: peoples[indexPath.row]) return cell } return UITableViewCell() } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let vc = PeopleDetailVC() let selectedPeople = peoples[indexPath.row] vc.people = selectedPeople vc.modalPresentationStyle = .overCurrentContext vc.modalTransitionStyle = .crossDissolve present(vc, animated: true, completion: nil) } // func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { // // return tableView.estimatedRowHeight // } // func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { // // print(preferredContentSize.height) // print(tableView.contentSize.height) // print(tableView.contentSize) // // return 300 // } } //MARK:- getPeopleMethods extension PeopleVC { // By usinf the Reference Fetch the data func fetchCollection() { let collectionRef = db.collection("peoples").whereField("category", isEqualTo: category.id) listener = collectionRef.addSnapshotListener { (snap, error) in if let error = error { print(error.localizedDescription) return } self.peoples.removeAll() guard let documents = snap?.documents else { return } for document in documents { let data = document.data() let newPeople = People(data: data) self.peoples.append(newPeople) } self.tableView.reloadData() } } } |
As deist is used for removing listener in the above code.
we can use the deinitailzer for the cleaning up the Code. Please refer to my Other blog from here . For more information please refer to the Apple official from here.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.