AppDelegate
                             vs.
                     NSNotificationCenter
                         What they a...
What is a Delegate?
                    • An object that acts on events
                    • Generally named “ClassXYZDel...
Target-Action
                    • Target: the delegate class that implements a
                         method to call w...
Quick “How-To”
                            In your accessor class header:
                Class .h file:
                @i...
Quick “How-To”
                         Implement the code in the accessor class:
                Class .m file:
          ...
Quick “How-To”
                   Calling a delegate function from the accessor:
                Class .m file:
           ...
Quick “How-To”
                                     Alternate method:
                Class .m file:
                - (voi...
Delegate Use:
                    • A delegate is called after tapping a line in a
                         UITableView, o...
An example delegation
                                        Modal Confirmation Dialog button clicked,
                   ...
What is an
                               AppDelegate?
                    • Serves as the root controller of your
       ...
AppDelegate Example
                 Delegate functions in an example AppDelegate:
                - (void)applicationDidF...
Calling the
                 AppDelegate members:
                    • First, get access to the AppDelegate by
          ...
Call the AppDelegate
                               members:
                - (void)alertView:(UIAlertView *)alertView
  ...
Delegate Pros
                    • Function declarations must be strictly
                         defined, or they will n...
Delegate Cons
                    • If a delegate method is not defined
                         properly, it will not be p...
AppDelegate Pros:
                    • Provide direct access to the application’s
                         view controlle...
AppDelegate Overuse:
                    • AppDelegates tend to get filled up with too
                         many member...
How I’ve used
                               Delegates
                    • Controlled application flow by switching
     ...
What is a Notification?
                    • A queued message with a payload
                    • Sent application-wide, ...
Setting up the observer
                            This class receives an event message:
                Class .m file reg...
Sending the notification
                     This function sends the notification message:
                Class .m file sen...
Don’t forget to
                                 deregister!
                Class .m file registering the observer:
      ...
Example Notifications
                    • Sending an application-wide status change
                         or refresh m...
Notification Flow




Tuesday, March 3, 2009
Notification Flow
                    Notification
                     + Payload




Tuesday, March 3, 2009
Notification Flow
                    Notification
                     + Payload




Tuesday, March 3, 2009
Notification Flow
                    Notification
                                  NSNotificationCenter
                   ...
Notification Flow
                    Notification
                                  NSNotificationCenter
                   ...
Notification Flow
                    Notification
                                             NSNotificationCenter
        ...
Notification Flow
                    Notification
                                             NSNotificationCenter
        ...
Notification Flow
                    Notification
                                             NSNotificationCenter
        ...
Notification Flow
                    Notification
                                             NSNotificationCenter
        ...
Notification Flow
                    Notification
                                             NSNotificationCenter
        ...
Notification Flow
                    Notification
                                             NSNotificationCenter
        ...
Notification Flow
                    Notification
                                             NSNotificationCenter
        ...
Notification Flow
                    Notification
                                             NSNotificationCenter
        ...
Notification Flow
                    Notification
                                             NSNotificationCenter
        ...
Notification Flow
                    Notification
                                             NSNotificationCenter
        ...
Notification Flow
                    Notification
                                             NSNotificationCenter
        ...
A real-world example
                                               CoreLocationUpdate


                                 ...
Notification Pros
                    • “Fire and Forget”
                    • A single notification can be processed by
  ...
Notification Cons
                    • If you don’t de-register a handler, you could
                         process a no...
How I’ve used
                              Notifications
                    • Wrote an application-wide CoreLocation
    ...
Delegate + Notification
                        Scenario:
                    • SQL Insert/Update service listens for
     ...
Notification Extras:
                    • You can enqueue notifications using
                         “enqueueNotification”...
Things to think about...
                    • Cocoa touch uses a large combination of
                         delegates ...
A Quick Demo!!!

                    • Simple demo that shows
                         NSNotificationCenter being overridde...
Thank you!
                    • khollis@willowtreeconsulting.com
                    • http://www.bitgatemobile.com/
    ...
Upcoming SlideShare
Loading in...5
×

NSNotificationCenter vs. AppDelegate

36,569

Published on

Kenji Hollis - NSNotificationCenter vs. AppDelegate. Non game App Dev Track. 360|iDev San Jose '09

Published in: Technology, News & Politics
2 Comments
38 Likes
Statistics
Notes
No Downloads
Views
Total Views
36,569
On Slideshare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
441
Comments
2
Likes
38
Embeds 0
No embeds

No notes for slide

NSNotificationCenter vs. AppDelegate

  1. 1. AppDelegate vs. NSNotificationCenter What they are, and some Pros and Cons Kenji Hollis March 03, 2009 WillowTree Consulting Group Tuesday, March 3, 2009
  2. 2. What is a Delegate? • An object that acts on events • Generally named “ClassXYZDelegate” • Allows objects to manipulate one-another without the need for inheritance • Used by most Cocoa Touch classes • Should be implemented using formal protocols • Think of them as “callbacks”, most of which are optional. Tuesday, March 3, 2009
  3. 3. Target-Action • Target: the delegate class that implements a method to call when an event is performed. • Action: the method that responds to the target event. • Used by UIKit. Example, UIAlertView: set the delegate (target), implement dismissal delegate and respond to the request (action). Tuesday, March 3, 2009
  4. 4. Quick “How-To” In your accessor class header: Class .h file: @interface Class { id parentDelegate; } - (id)delegate; - (void)setDelegate:(id)newDelegate; Tuesday, March 3, 2009
  5. 5. Quick “How-To” Implement the code in the accessor class: Class .m file: - (id)delegate { return parentDelegate; } - (void)setDelegate:(id)newDelegate { parentDelegate = newDelegate; } Tuesday, March 3, 2009
  6. 6. Quick “How-To” Calling a delegate function from the accessor: Class .m file: - (void)performEvent { if ([parentDelegate respondsToSelector:@selector(myEvent)]) { [parentDelegate myEvent]; } } We use “respondsToSelector” to verify that the selector exists in the implementing delegate class. Note the “blind call” to the “myEvent” function. Tuesday, March 3, 2009
  7. 7. Quick “How-To” Alternate method: Class .m file: - (void)performEvent { if ([parentDelegate respondsToSelector:@selector(myEvent)]) { [parentDelegate performSelector:@selector(myEvent)]; } } We are calling the selector by using “performSelector” instead of blindly calling the member. Here, “myEvent” is the action, “parentDelegate” is the target. Tuesday, March 3, 2009
  8. 8. Delegate Use: • A delegate is called after tapping a line in a UITableView, or tapping a button in a UIAlertView. • Telling the application to switch to a different tab view. • Responding to an application interruption. • Running an operation that may block the application until complete (with a “Please wait” box) Tuesday, March 3, 2009
  9. 9. An example delegation Modal Confirmation Dialog button clicked, generates “OK” or “Cancel” action event. “OK” Clicked event “Cancel” Clicked event Display progress pane “Please wait ...” Store “Cancel” result in the DB HTTP Post sent synchronously Close modal dialog to record confirmation Wait for response from the HTTP Post Parse response, store result in DB, close modal dialog and progress pane. Tuesday, March 3, 2009
  10. 10. What is an AppDelegate? • Serves as the root controller of your application. • Contains methods to handle application start, shutdown, and interruption. • Contains the setup code for your application’s main window. • Generally knows how to control your application from the top level (ie. change tab bar views, save prefs, etc.) Tuesday, March 3, 2009
  11. 11. AppDelegate Example Delegate functions in an example AppDelegate: - (void)applicationDidFinishLaunching:(UIApplication *)app { // ... Set up your code here ... } - (void)function_1 { // ... Do something in the app view controller ... } - (void)function_2 { // ... Manipulate the tab controller here ... } - (void)function_3 { // ... Miscellaneous call here ... } - (void)function_4; - (void)function_5; // ... and on ... and on ... and on... Tuesday, March 3, 2009
  12. 12. Calling the AppDelegate members: • First, get access to the AppDelegate by grabbing the [[UIApplication sharedApplication] delegate] object. • Cast that to the application object. • Call the delegate methods where appropriate. Tuesday, March 3, 2009
  13. 13. Call the AppDelegate members: - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)button { // Retrieve the application delegate MyApplication *myApp = (MyApplication *) [[UIApplication sharedApplication] delegate]; // Call the application delegate method. [myApp function_1]; } Tuesday, March 3, 2009
  14. 14. Delegate Pros • Function declarations must be strictly defined, or they will not be performed • Delegate functions are almost always guaranteed to fire when the selector is called • Delegates can be reused as often as the programmer sees fit, from several classes • Often used in conjunction with modal operations, so they’re easier to debug • Can be called asynchronously. Tuesday, March 3, 2009
  15. 15. Delegate Cons • If a delegate method is not defined properly, it will not be performed, or it may crash the app if called blindly. • If a delegate method signature changes, it has to be changed in all classes that call it. • Because most functions are called synchronously, they can tie up the app if expensive operations are performed. Tuesday, March 3, 2009
  16. 16. AppDelegate Pros: • Provide direct access to the application’s view controllers and layout control. • Provide simple, direct access to the root application. • Controls how the application responds to interruptions, startup, and shutdown. This is provided to you by the UIApplicationDelegate protocol. Tuesday, March 3, 2009
  17. 17. AppDelegate Overuse: • AppDelegates tend to get filled up with too many member functions, so the AppDelegate turns into a giant mess. • People often use the AppDelegate to get access to their view controller functions. • Miscellaneous functions are often placed in the AppDelegate when they could be otherwise placed in a utility class. Tuesday, March 3, 2009
  18. 18. How I’ve used Delegates • Controlled application flow by switching tab views at the application top-level. • Swapped views when the application changes from portrait to landscape mode. • Initialized the application by copying a read- only database into a read/write directory. • Preloaded data from a remote web call on application first load. • The list goes on. Tuesday, March 3, 2009
  19. 19. What is a Notification? • A queued message with a payload • Sent application-wide, not to a specific class • Posted “Immediately”, asynchronously, or when convenient to the OS • A single queued message can be received and processed by multiple observers • Can be sent with no registered observers, and your application won’t crash! Tuesday, March 3, 2009
  20. 20. Setting up the observer This class receives an event message: Class .m file registering the observer: - (id)init ... { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selector1:) name:@”MY_SELECTOR_1” object:nil]; } - (void)selector1:(NSNotification *)notification { NSObject *obj = [notification object]; if ([obj isKindOfClass:[NSDictionary class]]) { // ... Do dictionary payload functionality here. } else { // ... Perform non-object payload functionality here. } } Tuesday, March 3, 2009
  21. 21. Sending the notification This function sends the notification message: Class .m file sending the notification: - (void)myNotifier { [[NSNotificationCenter defaultCenter] postNotificationName:@”MY_SELECTOR_1”]; NSDictionary *myDictionary = [NSDictionary dictionaryWithContentsOfFile:@”myFile.plist”]; [[NSNotificationCenter defaultCenter] postNotificationName:@”MY_SELECTOR_1” object:myDictionary]; } Tuesday, March 3, 2009
  22. 22. Don’t forget to deregister! Class .m file registering the observer: - (void)dealloc { [super dealloc]; [[NSNotificationCenter defaultCenter] removeObserver:self name:@”MY_SELECTOR_1” object:nil]; } Note: If you don’t de-register the observer, the class will receive multiple copies of the notification. Tuesday, March 3, 2009
  23. 23. Example Notifications • Sending an application-wide status change or refresh message • Notifying the application when a network change occurs • When network data or a packet is received and needs to be processed asynchronously • Sending a message to more than one listener at a time Tuesday, March 3, 2009
  24. 24. Notification Flow Tuesday, March 3, 2009
  25. 25. Notification Flow Notification + Payload Tuesday, March 3, 2009
  26. 26. Notification Flow Notification + Payload Tuesday, March 3, 2009
  27. 27. Notification Flow Notification NSNotificationCenter + Payload Tuesday, March 3, 2009
  28. 28. Notification Flow Notification NSNotificationCenter + Payload Tuesday, March 3, 2009
  29. 29. Notification Flow Notification NSNotificationCenter + Payload Observer Tuesday, March 3, 2009
  30. 30. Notification Flow Notification NSNotificationCenter + Payload Observer Tuesday, March 3, 2009
  31. 31. Notification Flow Notification NSNotificationCenter + Payload Observer Operation(s) Tuesday, March 3, 2009
  32. 32. Notification Flow Notification NSNotificationCenter + Payload Observer Operation(s) Tuesday, March 3, 2009
  33. 33. Notification Flow Notification NSNotificationCenter + Payload Observer Observer Operation(s) Tuesday, March 3, 2009
  34. 34. Notification Flow Notification NSNotificationCenter + Payload Observer Observer Operation(s) Tuesday, March 3, 2009
  35. 35. Notification Flow Notification NSNotificationCenter + Payload Observer Observer Observer Operation(s) Tuesday, March 3, 2009
  36. 36. Notification Flow Notification NSNotificationCenter + Payload Observer Observer Observer Operation(s) Tuesday, March 3, 2009
  37. 37. Notification Flow Notification NSNotificationCenter + Payload Observer Observer Observer Operation(s) Operation(s) Tuesday, March 3, 2009
  38. 38. Notification Flow Notification NSNotificationCenter + Payload Observer Observer Observer Operation(s) Operation(s) Tuesday, March 3, 2009
  39. 39. Notification Flow Notification NSNotificationCenter + Payload Observer Observer Observer Operation(s) Operation(s) Operation(s) Tuesday, March 3, 2009
  40. 40. A real-world example CoreLocationUpdate NSNotificationCenter Observer 1 Observer 2 Observer 3 Hit geonames.org to Store GPS data in Calculate speed and cross-reference GPS SQL database bearing Update label on Update label on screen with data screen with data All of these operations happen asynchronously. Tuesday, March 3, 2009
  41. 41. Notification Pros • “Fire and Forget” • A single notification can be processed by multiple listeners • Notifications can be posted to the queue with different “posting styles” or priorities • Notifications can be coalesced so they can be sent in pairs or alphabetically • Almost always used asynchronously. Tuesday, March 3, 2009
  42. 42. Notification Cons • If you don’t de-register a handler, you could process a notification message multiple times. (Typically, de-registration is done in the dealloc member function.) • Sending asynchronous notifications can be hard to debug, like threads. • The order in which notifications are received by registered listeners is not guaranteed. Tuesday, March 3, 2009
  43. 43. How I’ve used Notifications • Wrote an application-wide CoreLocation wrapper that notifies when location range changes (remember the real-world example?) • Sent notifications to update multiple views when a database change occurred. • Sent notifications when XML lines of data are parsed one-at-a-time. Tuesday, March 3, 2009
  44. 44. Delegate + Notification Scenario: • SQL Insert/Update service listens for notifications, with a payload containing insert or update SQL data. • When the SQLUpdate call completes, it fires off a new notification indicating new data has been populated. • A Notification is received, and the affected UITableView then calls “refreshData” and updates itself with the new SQL data, using delegates to populate and refresh itself. Tuesday, March 3, 2009
  45. 45. Notification Extras: • You can enqueue notifications using “enqueueNotification” for finer notification control. • You can create your own NSNotificationCenter queue for even finer control. • You can pair notifications up alphabetically or by object for “in order” event firing. Tuesday, March 3, 2009
  46. 46. Things to think about... • Cocoa touch uses a large combination of delegates and notification calls. • You can see lots of fun things the UI does by using Categories on the NSNotificationCenter class. • If you end up using NSNotificationCenter in your app, try experimenting with the “enqueueNotification” function for finer grained control. Tuesday, March 3, 2009
  47. 47. A Quick Demo!!! • Simple demo that shows NSNotificationCenter being overridden with Categories, so we can see those “fun things” Tuesday, March 3, 2009
  48. 48. Thank you! • khollis@willowtreeconsulting.com • http://www.bitgatemobile.com/ • Twitter: @KenjiHollis • kenji.hollis on Skype • 303-521-1864 • Questions or comments? Speak up!! Tuesday, March 3, 2009
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×