{"id":416,"date":"2008-08-26T10:49:43","date_gmt":"2008-08-26T17:49:43","guid":{"rendered":"http:\/\/www.corbinstreehouse.com\/blog\/index.php\/2008\/08\/cocoa-programmers-avoid-writing-to-the-user-defaults-when-you-dont-need-to\/"},"modified":"2018-10-31T10:15:16","modified_gmt":"2018-10-31T17:15:16","slug":"cocoa-programmers-avoid-writing-to-the-user-defaults-when-you-dont-need-to","status":"publish","type":"post","link":"https:\/\/www.corbinstreehouse.com\/blog\/2008\/08\/cocoa-programmers-avoid-writing-to-the-user-defaults-when-you-dont-need-to\/","title":{"rendered":"Cocoa programmers: avoid writing to the user defaults when you don&#8217;t need to"},"content":{"rendered":"<p id=\"top\" \/>\n<p>[Edit: ecto ate this post, so I&#8217;m typing it in again!]<\/p>\n<p>I discovered that a lot of applications will unnecessarily write to NSUserDefaults. This causes your app to hit the disk when it shouldn&#8217;t, and is a slight performance penalty. AppKit is also susceptible to this problem; if you hit cmd-O to bring up the open panel in any application, you will see it writing things to the user defaults, when it probably doesn&#8217;t need to do so. I&#8217;m working on fixing that, and you should to!<\/p>\n<p>So, how do you do it? It is easy &#8212; just add a breakpoint on -[NSUserDefaults(NSUserDefaults) setObject:forKey:]. You can do this with gdb:<\/p>\n<p><span style=\"font-family: Courier;\">b -[NSUserDefaults(NSUserDefaults) setObject:forKey:]<\/span><\/p>\n<p>Or you can use the breakpoints window in Xcode (my preferred way):<\/p>\n<p><a href=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2008\/08\/picture4-breaking-on-user-defaults.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2008\/08\/picture4-breaking-on-user-defaults-tm.jpg\" width=\"450\" height=\"260\" alt=\"Picture4 - breaking on user defaults.png\" \/><\/a><\/p>\n<p>Then, reproduce whatever action might cause it to happen (ie: starting your application, or in my test case, cmd-o to bring up the open panel). Look at the backtrace in Xcode and figure out why you are doing too much work:<\/p>\n<p>\n<a href=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2008\/08\/picture5-the-callstack-for-a-user-default.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2008\/08\/picture5-the-callstack-for-a-user-default-tm.jpg\" width=\"450\" height=\"324\" alt=\"Picture5 - the callstack for a user default.png\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Edit: ecto ate this post, so I&#8217;m typing it in again!] I discovered that a lot of applications will unnecessarily write to NSUserDefaults. This causes your app to hit the disk when it shouldn&#8217;t, and&#8230; <a class=\"read-more\" href=\"https:\/\/www.corbinstreehouse.com\/blog\/2008\/08\/cocoa-programmers-avoid-writing-to-the-user-defaults-when-you-dont-need-to\/\">[read more]<\/a><\/p>\n","protected":false},"author":1,"featured_media":417,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6,86],"tags":[63,130,72],"class_list":["post-416","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cocoa","category-coding","tag-cocoa","tag-objc","tag-programming"],"jetpack_featured_media_url":"https:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2008\/08\/picture4-breaking-on-user-defaults.jpg","jetpack_shortlink":"https:\/\/wp.me\/p8zn47-6I","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/comments?post=416"}],"version-history":[{"count":1,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/416\/revisions"}],"predecessor-version":[{"id":5173,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/416\/revisions\/5173"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media\/417"}],"wp:attachment":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media?parent=416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/categories?post=416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/tags?post=416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}