tech savvy
tech savvy

Reputation: 1435

"Delegates or NSNotifications" Adjudging performance of code?

In my application, I have to display image files as a list in tableview, present them in full size and as multiple thumbnails. Hence basically I developed three seperate classes to handle these three views. Now to perform any file operations, I can think of two approaches:

  1. Create appdelegate objects for all these classes, handle them accordingly. When one operation on a photo file is performed in one class, all other classes are notified using NSNotification, keeping the obeserver as Appdelegate object.

  2. Create locally objects for these classes as and when required and assign delegates for performing file operations from one class to other by calling relevant methods.

However, I was not able to judge Which approach would be better in terms of memory usage and performance? Thanks in advance.

Upvotes: 0

Views: 179

Answers (2)

drekka
drekka

Reputation: 21883

It depends a lot on the code and how you are structuring your app. I general use delegates in the following situation:

  • Where the delegate object exists before and after the main object that needs it. In other words the main object does not need to worry about the lifecycle of it's delegate.
  • Where the relationship between an object and it's delegate object is a strict one to one. In other words only one delegate object needs to interact with the main object. I have seen situations where delegates are swapped in and out and I would not recommend such code.
  • Where the main object needs information from the delegate.

I would use notifications where:

  • Multiple objects need to know of about things happening in another class.
  • Where the main class does not need to interact with the other classes or even know they exist.

Which ever you choose I would not have more than one file management object for each image. The simple reason being that having multiple means you need to ensure that they all have the same state and therefore are communicating with each other. Otherwise bugs will creep in.

Upvotes: 0

justin
justin

Reputation: 104698

Using a one-to-one relationship with direct messaging is the simpler relationship and means of communication/messaging. Favor the delegate callback -- Number 2.

It is also easy to make this design bidirectional -- if the view goes offscreen, you could perform a cancellation. If the load fails, it is easier to inform the controller.


NSNotifications are comparably heavyweight. Not necessary.

Storing a bunch of stuff in a singleton (app delegate) can result in several unnecessarily retained objects. If your program is concurrent, then that can add even more complexity. There's no need for any of this complexity or introduction of mutable global state, and there is no reason presented whereby the objects should have a much larger scope of access and lifetime.

You can optimize for specific needs beyond that, but I don't see any at this time.

Upvotes: 1

Related Questions