3 ms·
Very much agree, almost every time I've seen custom notifications they've been a huge source of pain across the app.
by fleitz 11y ago
Very much agree, almost every time I've seen custom notifications they've been a huge source of pain across the app.
- cageface 11y agoCode that relies on notifications for control flow is very hard to debug and reason about. Much better to set up explicit delegate/observer protocols between parts of your app that need to communicate with each other. You can use something like NSHashTable to manage weak references to a collection of observers that conform to your delegate protocol.
- increment_i 11y agoBut again, like the author said, they're pretty useful for truly global events from the OS itself. For instance, when the keyboard notification fires, it comes with useful information the app can use at runtime like xy coordinates involving the keyboard which can be used to make calculations on your views independent of the device size. Edit: Sorry, just noticed your use of the qualifier "custom" there.
- amm 11y ago(Global) notifications are sometimes the only sane way to deliver a message from one part of your application to another one without explicitly wiring them together. If you're working with some kind of plugin system they're also incredibly useful to deliver state updates to plugged-in components. Once your codebase reaches a certain size, an event bus is a very powerful pattern that, if applied correctly, will keep your classes/functions/... decoupled. Naturally, there is a huge potential for misuse, but that argument probably applies to any design pattern.