In the Realm documentation for Swift, the section on notifications has this sample code:
class ViewController: UITableViewController {
var notificationToken: NotificationToken? = nil
override func viewDidLoad() {
super.viewDidLoad()
let realm = try! Realm()
let results = realm.objects(Person.self).filter("age > 5")
// Observe Results Notifications
notificationToken = results.addNotificationBlock { [weak self] (changes: RealmCollectionChange) in
guard let tableView = self?.tableView else { return }
// ... some code removed here ...
}
}
deinit {
// notificationToken?.invalidate() in Realm 10.12
notificationToken?.stop()
}
}
I was wondering why [weak self]
is used here instead of [unowned self]
. In what use case could self
be nil here? (before reaching deinit
)