Returning statuses from MGTwitterEngine - iphone

I am using MGTwitterEngine in an iPhone application to make a simple call for user statuses. The engine has a delegate method:
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)identifier
And I can see my statuses logged in the console. My question is what is the best way to then use the statuses and be informed when they have been received?
I suspect this might be more about how to use the delegate pattern properly.
Thanks,

I went with setting up an NSNotification observer and then calling that from statusesReceived:forRequest. I also set an NSArray iVar in the delegate method which I access in my NSNotification callback:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(tweetNotificationHandler:) name:#"tweetsArrived" object:nil];
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)identifier{
tweets = statuses; //this is an ivar
NSNotification* notification = [NSNotification notificationWithName:#"tweetsArrived" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
-(void)tweetNotificationHandler: (NSNotification*)notification {
//do your results handling here.
}

Related

How do I retrieve the notification object for PayPalTransactionDidSucceedNotification

The documentation says "The notification's object is the completed PayPalPayment, same as the delegate method."
But I still don't get it. This is how I add the notification:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(payPalCompletionHandler:)
name:PayPalTransactionDidSucceedNotification
object:nil];
-(void)payPalCompletionHandler:(NSNotification *) notification
{
// How do I get (PayPalPayment *)completedPayment?
}
Answering my own question, duh.
use this to get the notifications object.
NSDictionary *myObject = [notification object];

selector from inside a delegate method triggers twice

A weird issue occurs after a delegate method of my static lib is fired. first of all, the project has a sub project which is a static library (xcode 4.6 ios 6.x). The static lib fires its own delegates according to the event.
the App implements the delegate method of the static lib. in the implementation i use the following to access the UI elements and trigger other events. Didgetnotified is the delegate method of the lib.
- (void)didGetNotified
{
dispatch_async(dispatch_get_main_queue(), ^{
[self parseData];
NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter];
[notifyCenter addObserver:self
selector:#selector(updateUI)
name:#"updateUIN"
object:nil];
});
}
-(void) parseData {
//parse data and its ready now and send notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateUIN" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"updateUIN" object:nil];
}
-(void) updateUI {
//this method gets fired twice mostly
}
the problem is that the updateUI gets called twice. i cant see what i'm doing wrong. is it something with the threading? the static lib delegate is not on the main thread. but i use the dispatch on the main thread. can some one please explain?
thank in advance.
after intensive debugging i have found that the adding oberserver was actually happened twice. the solution was to remove the oberserver before adding it in case of WIFI disconnects and the date stream goes thru 3G and that case my delegate was fired twice and registered oberver 2 times.
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"updateUIN" object:self];
NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter];
[notifyCenter addObserver:self
selector:#selector(updateUI)
name:#"updateUIN"
object:self];

Can I watch an NSNotification from another class?

I'm trying to get my head around NSNotificationCenter. If I have something like this in my App Delegate:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(something:)
name:#"something"
object:nil];
-----
-(void)something:(NSNotification *) notification
{
// do something
}
Can I somehow watch this in another view controller? In my case, I'd like to watch it in a view controller with a table, and then reload the table when a notification is received. Is this possible?
Yes you can do it like this:
In class A : post the notification
[[NSNotificationCenter defaultCenter] postNotficationName:#"DataUpdated "object:self];
In class B : register first for the notification, and write a method to handle it.
You give the corresponding selector to the method.
//view did load
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleUpdatedData:) name:#"DataUpdated" object:nil];
-(void)handleUpdatedData:(NSNotification *)notification {
NSLog(#"recieved");
}
Yes you can that is the whole purpose of NSNotification, you just have to add the View Controller you want as an observer exactly the same way you did on your App Delegate, and it will receive the notification.
You can find more information here: Notification Programming
Of course it's possible, that's the whole point of notifications. Using addObserver:selector:name:object: is how you register to receive notifications (you should do this in your table view controller), and you can use postNotificationName:object:userInfo: to post a notification from any class.
Read Notofication Programming Topics for more info.
You can register to observe notifications in as many classes as you like. You simply need to "rinse and repeat". Include the code to register as an observer in your view controller (perhaps in viewWillAppear:) and then reload the tableView from your method:
- (void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(something:) name:#"something" object:nil];
}
-(void)something:(NSNotification *) notification
{
[self.tableView reloadData];
}
It's also a good idea to de-register the view controller once you no longer need the notifications:
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
You should just add that as an Observer and give a different selector method if you want that viewController to behave differently when that notification is posted.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(somethingOtherThing:)
name:#"something"
object:nil];
-(void)somethingOtherThing:(NSNotification *) notification
{
// do something
}

Does NSNotification work everywhere?

I'm sending a notification using:
[[NSNotificationCenter defaultCenter] postNotificationName:#"historyLoaded" object:jsonReturn];
And receiving the notification using:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(manageHistory:) name:#"historyLoaded" object:nil];
Then the method in the selector is:
- (void) manageHistory: (NSNotification *) historyData{
NSLog(#"this bit of code was run");
}
For some reaason the notification doesn't get through. Can notifications be send and received from anywhere in the app?
The object parameter in postNotification should be filled with an object which is "sending" the notification, or nil if the sender is not necessarily specified.
If you want to pass some information along, you should use postNotificationName:object:userInfo and put the information in userInfo dictionary instead.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(manageHistory) name:#"historyLoaded" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"historyLoaded"
object:nil userInfo:jsonReturn];
- (void) manageHistory: (NSNotification *) historyData{
NSDictionary* _dict = historyData.userInfo;
NSLog(#"Your information embedded to dictiuonary obj %#",_dict);
}
NOTE : Make sure your historyData should be a dictionary object in postNotificationName

iPhone - Launching selectors from a different class

I'd like to reload a table view which is in another class called "WriteIt_MobileAppDelegate" from one of my other classes which is called "Properties". I've tried to do this via the NSNotificationCenter class - the log gets called but the table is never updated.
Properties.h:
[[NSNotificationCenter defaultCenter] postNotificationName:#"NameChanged"
object:[WriteIt_MobileAppDelegate class]
userInfo:nil];
WriteIt_MobileAppDelegate.m
-(void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadItProperties:)
name:#"NameChanged" object:self];
}
- (void) reloadItProperties: (NSNotification *)notification {
NSLog(#"Reloading Data"); //this gets called
[[self navigationController] dismissModalViewControllerAnimated:YES];
[self.navigationController popToRootViewControllerAnimated:YES];
[self.tblSimpleTable reloadData];
[self.tblSimpleTable reloadSectionIndexTitles];
// but the rest doesn't
}
What am I doing wrong here?
Seems like you are using the object parameter wrong:
addObserver:selector:name:object:
notificationSender
The object whose
notifications the observer wants to
receive;
that is, only notifications
sent by this sender are delivered to
the observer. If you pass nil, the
notification center doesn’t use a
notification’s sender to decide
whether to deliver it to the observer.