This may be a Realm bug, or OE, but I am having an issue setting up a realm observer in a modal window (not a sheet).
TL;DR
When using Realm Notifications, the .observe’s initial event fires correctly when a view controller is shown as a sheet, but does not fire when shown as a modal window until the window is actually closed.
Here’s the details;
Here’s the setup. I have a window with two buttons; button 0 creates a sheet from a viewController and displays it. button 1 does the same thing but displays it as a modal window.
It’s super simple but here’s the main view controller (MainVC)
and then the second SecondVC being shown as a sheet)
or as a modal window
The add observer code in the second view controller adds an observer to some objects
func addObserver() {
print(#function)
let realm = try! Realm()
let people = realm.objects(PersonClass.self)
// token is a class var token: NotificationToken?
self.token = people.observe { changes in
switch changes {
case .initial:
print("initial load")
case .update(_, _, _, _):
print("update")
case .error(let error):
fatalError("\(error)")
}
}
}
}
Here’s the issue:
When showing as a sheet and clicking the Add Observer button, as expected we can see the observer is added and the .initial
change fires (output shown in the console)
Realm version: 10.5.0
2021-01-15 14:33:11.501816-0500 Realm 5[33396:7693838] Metal API Validation Enabled
addObserver()
initial load
However, when performing the exact same steps when showing the SecondVC as a modal window, it does not fire the initial load
Realm version: 10.5.0
2021-01-15 14:35:17.940594-0500 Realm 5[33436:7696961] Metal API Validation Enabled
addObserver()
But if I then dismiss the SecondVC when it’s showing as a modal window, that event fires when it closes.
Is this a Realm thing or just me overlooking some inherent difference in how modal windows work?