You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## 📜 Description
Remove KVO when keyboard disappears.
## 💡 Motivation and Context
It seems like we can not attach a single listener to the keyboard when
it appeared for the first time. iOS may throw
`KVO_IS_RETAINING_ALL_OBSERVERS_OF_THIS_OBJECT_IF_IT_CRASHES_AN_OBSERVER_WAS_OVERRELEASED_OR_SMASHED`,
which means, that the memory for the listener was released and thus a
handler is corrupted (KVO was released while still observing the key
path).
To overcome the problem I've decided to remove observation when keyboard
becomes hidden. To detect the moment when keyboard got hidden I used
`UIWindow.didBecomeHiddenNotification` (since for attaching a KVO
listener I've used `UIWindow.didBecomeVisibleNotification` - the
motivation behind "why I've used these lifecycles" is the fact, that
these lifecycle methods will be triggered only one time when keyboard
appears/disappears, unlike other methods, which can be called several
times).
Also I've added a local variable called `hasKVObserver` and two helper
functions:
- `setupKVObserver`;
- `removeKVObserver`.
These functions control the value of `hasKVObserver` and assure that we
will not call `.removeObserver` if listener is not attached (such
situation will cause a crash and using `hasKVObserver` value we avoid
it).
Fixes#143
## 📢 Changelog
### iOS
- added `UIWindow.didBecomeVisibleNotification` listener;
- clean KVO when `UIWindow.didBecomeVisibleNotification` is emitted and
KVO is present;
## 🤔 How Has This Been Tested?
Tested on:
- iPhone 14 Pro (iOS 16.2, simulator);
- iPhone 6s (iOS 15.7, real device);
## 📝 Checklist
- [x] CI successfully passed
0 commit comments