I have a parser class and a view controller class. In the parser class i am sending a request and receiving an asynchronous response. I want to download the same several times but while sending different parameters each time. I have registered an observer in the class:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dataDownloadComplete:) name:OP_DataComplete object:nil];
and then post a notification in :
-(void)connectionDidFinishLoading:(NSURLConnection *)
connection method of the parser class.
[[NSNotificationCenter defaultCenter] postNotificationName:OP_DataComplete object:nil];
I can't enclose the observer inside a For loop. What better alternative is there? Please help. Thanks in advance.
You probably do not need a for loop at all. As you said you send a request asynchroniously, so just send it in your observer code instead of making a for loop. Then you get back to your observer after response is downloaded.
Related
I have the following code:
[[NSNotificationCenter defaultCenter] postNotificationName:kNewsfeedFetchCompleted object:self userInfo:userinfo];
only this, no where else. And here's how I set the observer:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newsfeedFetchCompleted:) name:kNewsfeedFetchCompleted object:nil];
question is when I do one post the newsfeedFetchCompleted is called twice.. how is this even possible?
This is possible when your code for addObserver is executed twice. The notification function will be called as many times as it is registered.
So make sure your code for adding observer is executed for once only. So, you can keep it in viewDidLoad or init method.
If you are putting it in viewWillAppear then remove observer in viewWillDisAppear.
before you add observer, make sure you remove the previous observer added.
[[NSNotificationCenter defaultCenter]removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newsfeedFetchCompleted:) name:kNewsfeedFetchCompleted object:nil];
It is possible if you have added the same observer multiple times for the newsfeedFetchCompleted notification. You should match your addObserver calls with removeObserver calls.
For example if you added the observer in viewWillAppear/viewWillDidAppear/ViewDidLoad of a UIViewController, you should remove it in viewWillDisappear/viewDidDisappear/ViewDidUnload.
The corresponding remove call for addObserver, is removeObserver:name:object:
More info can be found in the NSNotificationCenter docs
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationForbidAdvPost object:nil];
I define postNotificationName:kNotificationForbidAdvPost in a .h file
Im my subclass of NSApplication, I import this .h file. And have a observer of this Notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(changeToRecordingStatus) name:kNotificationForbidAdvPost object:nil];
I am sure that the observer was added first, and then post the notification.
And I see the [UINotification default] in Debug. It have same memory address in two part of code.
Is there some mistake I have?
Thanks!
The documentation is pretty clear when it states that
The method specified by
notificationSelector must have one and
only one argument (an instance of
NSNotification).
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000219-CHDCIGGB
So, I see your selector you are registering is changeToRecordingStatus when it must be changeToRecordingStatus:
- (void) changeToRecordingStatus: (NSNotification *) notification;
I have try that add a arugument, but it is still not worked.
I create a new project and use a no-argument function to response Notification, it is worked normally.
The reason why I cannot receive the Notification is that a removeObserver method was called at one part which I didn't notice.
Thanks for all your reply.
I need to pass some data to a method which I am registering to execute once I receive a notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:withCell:) name:UIKeyboardWillShowNotification object:nil];
Here I want to send some data for withCell part of my method.
How can I achieve this?
Short answer: you can't. You're registering for a notification and you can't control what is sent with that notification.
What is it you would like to do?
When you're posting notification and want to pass some data use:
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo
but this is system notification in your case, so the best choice for you would be registering callback in your view controller that would be called when keyboard is shown (it receives only one parameter - NSNotification). You'll have to use some ivar (e.g. selectedCell) and process it in that callback.
i have 2 classes.
class1 gets some information through the net. when one of these informations comes, class1 has to send a notification to class2.
i understood it so that i have to put
[[NSNotificationCenter defaultCenter] postNotificationName:at"anyUserNotification" object:class2];
into class1
[[NSNotificationCenter defaultCenter] addObserver:self selector:atselector(anyInteraction:) name:dontKnowTheSense object:dunno2];
have i understood the object:class2 in the postnotification right ?
if yes: is it possible to make an anonymious notification, so that the sending class must not know, which and how many classes are listening ?
or - at least i think so, have i understood the whole notification incorrect ?
besides the notification i dont need to pass any data, but in one case it would be helpful to pass an enum
so, could anybody please help me ?
btw: i cant find a way to post an at on this windows-pc and i dont know, why it did not indent the code, i think i made 4 spaces
Object is not a mandatory argument you can set it to nil or the object sending the notification message.
[[NSNotificationCenter defaultCenter] postNotificationName:#"NotificationName" object:notificationSenderOrNil];
When listening you can filter to only do something for notification sent by a specific sender.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(sel) name:#"NotificationName" object:notificationSenderOrNil];
And you can pass your data in a dictionnary with userInfo: argument.
Is it Ok with that ?
i am new Notification.what is the purpose of it...
can we use [[NSNotificationCenter defaultCenter] addObserver without postNotification,
If we can do it,how it is identified?what is the relationship between addObserver and postNotification?any one can explain in detail?
it's rather straight forward: you define an event by giving it a unique name, then you add an observer to it using [[NSNotificationCenter defaultCenter] addObserver and point it to a selector that receives the event.
then later on in your code you can postNotification and if the unique name matches the one you registered for - the selector you provided will be called.