Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of custom delegate vs NSNotification vs NSUserDefaults state

In my app I'm using NSUserDefaults to give a state then performing operations based on that state. In other places I'm using NSNotification-s to fire methods in other classes. I feel like for one example specifically it would be better to use a custom delegate.

What are the benefits and drawbacks to using NSNotification vs a custom delegate vs what I'm doing now with NSUserDefaults?

My question specifically intends to address any performance or potential issues between using NSUserDefaults to give state compared with simply calling methods using either protocols or NSNotificationCenter.

like image 301
Ethan Parker Avatar asked Dec 05 '25 04:12

Ethan Parker


2 Answers

It's important to remember that NSUserDefaults persists data. When you read and write from NSUserDefaults, you're actually reading and writing to/from disc. Whenever you use NSUserDefaults you should ask yourself "Is this something that needs to be persisted between app launches? Could/Should I do this thing without writing to disc?" (note about performance: any time you have to go to disc for something, expect that to take a much longer time)

NSUserDefaults is ideal for things like app settings. Does your app have multiple color schemes the user can choose from? Store those preferences in user defaults and look them up later.

I would put NSUserDefaults in a different category from the other communication patters, like delegation, notifications, blocks, KVO, target-action.

Here's an awesome article about communication patterns in iOS: http://www.objc.io/issue-7/communication-patterns.html . This goes into detail on each one and what they do, and I've found their flow-charts really useful. This article also talks about KVO (key-value observing) and blocks (closures in Swift).

Delegate:

Delegate

One big difference between the two, where the logic branches in the flow-chart, is whether the recipient knows the sender. You'll often hear notifications talked about as a one-to-many communication where as delegation is one-to-one.

Notifications: enter image description here

like image 93
erparker Avatar answered Dec 07 '25 19:12

erparker


Notification is used to broadcast messages to possibly several recipients unknown from the sender.

Delegation is used to send messages to a single known recipient acting on behalf of the sender.

Considering the performance is a good idea (delegation better for small number of notified objects, notification centre better for larger number of objects, or is it? run a profiler) but I think a more important factor since you are talking about Objective-C and less likely to be talking about the really high performance parts of your code base, which are likely to be written in C, is reducing compile-time dependencies between modules.

like image 22
Jessica Avatar answered Dec 07 '25 19:12

Jessica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!