Dismissing events through multiple view dispatchers from a stream in appdelegate

I have an NSAutoreleasePool thread that is designed to output information from a web service, my web service code works well, and I can run the thread to run in the view manager without any problems, in fact its work is pretty nice.

I want to:

  • move stream creation to appDelegate - easy!
  • run it periodically and somehow inform the viewing manager under it (5 - 10) if new information is loaded.
  • have the ability to manually execute a thread outside the scheduler

I can run the appdelegate method using the performSelectorOnMainThread function, but how can I make the controllers of my child view subscribe to the appdelegate method?

+3
multithreading event-handling objective-c iphone
Apr 25 '11 at 23:27
source share
3 answers

Using NSNotificationCenter, you can post messages well, notifications: D Thus, without adding appDelegate to other classes, other classes can β€œsubscribe” to the notifications they need.

In addition, I would keep the stream alive, each time creating a new stream, it is expensive only if it often occurs. I would recommend using GCD (iOS 4+)

+3
Apr 25 2018-11-21T00:
source share

Here is what you do:

From the class sending the message, send a notification, for example:

[[NSNotificationCenter defaultCenter] postNotificationName: @"YOUR_NOTIFICATION_NAME" object: anyobjectyouwanttosendalong(can be nil)]; 

In view controllers in which you want to be notified of a notification when sending:

In viewDidLoad, do:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(METHOD_YOU_WANT_TO_INVOKE_ON_NOTIFICATION_RECEIVED) name:@"YOUR_NOTIFICATION_NAME" object:sameasbefore/nil]; 

Attention! Do not forget about it in your view of DidUnload ():

 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"YOUR_NOTIFICATION_NAME" object:sameasbefore/nil]; 

I’m not very sure about the object associated with notifications, but you can see what is here

NOTE. When only one object notifies another, you are better off using protocols :) But in this case, since there are several view controllers, use notifications

+3
Apr 25 '11 at 23:40
source share

Use NSNotificationCenter to dispatch events observed by observers of your species?

+1
Apr 25 2018-11-11T00:
source share



All Articles