Debugging NSNotificationCenter
source link: https://www.timekl.com/blog/2018/02/22/debugging-nsnotificationcenter/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Debugging NSNotificationCenter
The debugDescription
property has been around for quite awhile. Formally first
appearing as a @property
on NSObject in iOS 5 and macOS
10.8, it came across into Swift as a member of the
CustomDebugStringConvertible
protocol, and continues to be
incredibly useful today.
But somehow it still manages to surprise me when I find a detailed custom
implementation of the property, like the one I just discovered on
NSNotificationCenter
.
I don’t remember why I decided to try printing plain old
NSNotificationCenter.defaultCenter
in lldb, but I do remember being very
surprised at the output. Rather than a plain object class-name-and-pointer pair
like most things return, I found a long list of registered notification
observers, complete with column headers:
(lldb) po NSNotificationCenter.defaultCenter
<NSNotificationCenter:0x6140000da9b0>
Name, Object, Observer, Options
com.apple.accessibility.reduce.motion.status, 0x1055e61d0, 0x7f8df5300260, 1001
com.apple.accessibility.asst.scanner.status, 0x1055e61d0, 0x7f8df5300260, 1001
UIAccessibilitySingleSystemColorChangedNotification, 0x1055e61d0, 0x106d19998, 1400
com.apple.accessibility.button.shapes, 0x1055e61d0, 0x7f8df5300260, 1001
UIAccessibilityReduceMotionStatusDidChangeNotification, 0x1055e61d0, 0x60800005bc00, 1400
…
This was way more helpful than I had dared hope for: every observer, the
object it’s observing, and the name of the notification it’s waiting for. The
amount of introspection here gives KVO’s observationInfo
a run for its money.
The one downside is that I don’t have a great affinity for matching pointer addresses by sight, especially in the ocean of hexadecimal produced by sixty-plus lines of this output. What’s more, you’re practically guaranteed at least that much debugger spew, since the system frameworks register for quite a few notifications before your code even gets run.
I had to cut through the noise… and what better way to process an app’s debugging info than with another app?
A couple hours later, NotificationDebugger was born: a Mac app that takes a
table of pointers and corresponding names, plus a text dump of
NSNotificationCenter’s debugDescription
, and produces a table of only the
observations involving a named pointer.
Notification debugger screenshot
It’s definitely got my coder’s design sensibilities (i.e. no labels on anything, anywhere) – but it solved the problem I was facing, which is good enough for publication on GitHub. Find the source here, and let me know if this is useful to you!
Recommend
-
254
Litter Litter is a pretty printer library for Go data structures to aid in debugging and testing. Litter is provided by Litter named for the fact that it outputs literals, which you
-
167
-
93
The 16 JavaScript debugging tips you probably didn’t knowWhen it comes to debugging JavaScript, the faster the better. Here are our top tips for faster and more efficient debugging.
-
114
-
76
Preface I’m a big fan of Prometheus and Grafana. As a former SRE at Google I’ve learned to appreciate good monitoring, a...
-
134
-
80
HyperLog Android Overview A utility logger library for Android on top of standard Android Log class for debugging purpose. This is a simple library that will allow Android apps or library to store log in...
-
37
NSNotificationCenter这个东西作为iOS工程师想必都不陌生,但是有人可能连参数的意义都没搞明白,写这篇文章的目的不止是为了让不会用的人会用,更是为了让会用的人理解得更透彻。本篇文章主要是梳理NSNotificationCenter的特性和值得注意的地方,并且在后面结合对...
-
6
NSNotification &NSNotificationCenter Written by
-
4
Let's Build NSNotificationCentermikeash.com: just this guy, you know? Friday Q&A 2011-07-08: Let's Build NSNotificationCenter by Mike Ash No...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK