Latest 1.2
Homepage https://github.com/meiwin/NgNotificationProxy
License MIT
Platforms ios 5.0, requires ARC
Frameworks Foundation
Authors

Build Status

NgNotificationProxy is small objective-c library that provides better way to work with notifications from NSNotificationCenter.

Adding to your project

If you are using CocoaPods, add to your Podfile:

pod NgNotificationProxy

To manually add to your projects:

  1. Add NgWeakProxy.h, NgWeakProxy.m, NgWeakProxySubclass.h, NgNotificationProxy.h and NgNotificationProxy.m to your project.

NgNotificationProxy requires ARC.

Features

NgNotificationProxy is built to make delivering notifications to particular threads a trivial task.
See Delivering Notifications to Particular Threads.

In addition, NgNotificationProxy also detects if an observer reference is invalid and stop observing for notifications automatically (instead of crashing).

With NgNotificationProxy, you can specify target thread to which notification is delivered:

  • NgNotificationProxyThreadCurrent, deliver notifications to the same thread as where it is posted. This is default thread if not specified.
  • NgNotificationProxyThreadMain, deliver notifications to main thread.
  • NgNotificationProxyThreadBackground, deliver notifications to NgNotificationProxy’s background thread (thread name: NgNotificationProxy.internal.background).
  • Any other user-defined thread name.

Working with NgNotificationProxy

Deliver notifications to current thread.

[[NgNotificationProxy defaultProxy] addObserver:self 
                                       selector:@selector(onNotification:) 
                                           name:@"NotificationName" 
                                          object:nil];

Deliver notifications to main thread.

[[NgNotificationProxy defaultProxy] addObserver:self 
                                       selector:@selector(onNotification:) 
                                           name:@"NotificationName" 
                                         object:nil 
                                         thread:NgNotificationProxyThreadMain];

Deliver notifications to background thread.

[[NgNotificationProxy defaultProxy] addObserver:self 
                                       selector:@selector(onNotification:) 
                                           name:@"NotificationName" 
                                         object:nil 
                                         thread:NgNotificationProxyThreadBackground];

Deliver notifications to user-defined thread.

[[NgNotificationProxy defaultProxy] addObserver:self 
                                       selector:@selector(onNotification:) 
                                           name:@"NotificationName" 
                                         object:nil 
                                     threadName:@"MyThread"];

Stop observing notifications.

[[NgNotificationProxy defaultProxy] removeObserver:self];

or

[[NgNotificationProxy defaultProxy] removeObserver:self 
                                              name:@"NotificationName" 
                                            object:nil];

Benefits

NgNotificationProxy gives you better control and writing cleaner code.
One of the most common use case is to deliver notification to main thread for performing UI updates as result of an event.

With NSNotificationCenter.

// observing notification
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(updateUI:) 
                                             name:kUIDataUpdateNotification 
                                           object:nil];

// updateUI implementation.
- (void)updateUI:(NSNotification *)notification
{
  dispatch_async(dispatch_get_main_queue(), ^{ // ensure `reloadData` always execute in main-thread
     [self reloadData];
  });
}

With NgNotificationProxy.

// observing notification
[[NgNotificationProxy defaultProxy] addObserver:self
                                       selector:@selector(updateUI:) 
                                           name:kUIDataUpdateNotification 
                                         object:nil
                                         thread:NgNotificationProxyThreadMain];

// updateUI implementation
- (void)updateUI:(NSNotification *)notification
{
  [self reloadData];
}

Latest podspec

{
    "name": "NgNotificationProxy",
    "version": "1.2",
    "summary": "Simple proxy implementation for observing notification posted with `NSNotificationCenter`.",
    "homepage": "https://github.com/meiwin/NgNotificationProxy",
    "authors": {
        "Meiwin Fu": "[email protected]"
    },
    "source": {
        "git": "https://github.com/meiwin/ngnotificationproxy.git",
        "tag": "v1.2"
    },
    "description": "NgNotificationProxy is built to make delivering notifications to particular threads a trivial task.",
    "source_files": "NgNotificationProxy/*.{h,m}",
    "requires_arc": true,
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "frameworks": "Foundation",
    "platforms": {
        "ios": "5.0"
    }
}

Pin It on Pinterest

Share This