Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the delegate of MGTwitterEngine

I have setup and successfully logged in via xAuth using an extended class of MGTwitterEngine, my question is if I want to pass this to another view controller, how can I change the delegate class, as it is some sort of weak reference

@interface MGTwitterEngine : NSObject <MGTwitterParserDelegate> {
__weak NSObject <MGTwitterEngineDelegate> *_delegate;

Am I best wrap this up into a singleton class and pass around that way, seems overkill to login in each time, or have I missed a painstakingly obvious way of sharing this object around

At the moment I have added a setDelegate method to the MGTwitterEngine but feel as though I am fighting the framework unnecessarily

like image 410
kgutteridge Avatar asked Jun 02 '10 10:06

kgutteridge


2 Answers

If you're sharing the engine across multiple objects then you would want to have some other object/singleton wrap the engine and act as its sole delegate. If you've done database programming then think of it like a database connection -- you probably wouldn't have each view controller create its own database connection. Instead you'd create some sort of data manager object that is shared by the views and possibly abstracts away some of the DB internals.

If different view controllers handle different tasks -- like login, looking up users, querying messages, etc. then the delegate methods in your wrapper should be able to pass the responses along to the appropriate view controller.

If you have different view controllers calling the same methods (and if so, why?), you could still route responses back to the corresponding view controllers. As the MGTwitterEngine docs say, "Each Twitter API method returns an NSString which is a unique identifier for that connection." You would just need to pass an object (your view controller) or a block as an extra parameter to each of your wrapped methods. You can cache the twitter id string and this object/block in a mutable dictionary when your wrapper sends the response, then look up the connection id in the cache when it's time to handle the response.

like image 121
skue Avatar answered Oct 17 '22 01:10

skue


actually, you can. The delegate, is nothing but a variable in the MGTwitterEngine. Just add a instance of it in the next view controller adding the proper header and inplementation calls. after instatiating the new view controller set:

nextViewController._mgTwitterEngine = self.mgTwitterEngine;
nextViewController.mgTwitterEngine.delegate=nextViewController;

then call the nextViewController. Do not forget to set the delegate back to the original view controller when you return to it (either on viewDidAppear or viewWillAppear)

Hope that helps... Best Of luck!

like image 31
Marsson Avatar answered Oct 16 '22 23:10

Marsson