Change the delegate of MGTwitterEngine
Asked Answered
P

3

5

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

Palatial answered 2/6, 2010 at 10:37 Comment(1)
I would also love to know the answer to this one. I would have liked you to be able to specify the delegate on every request.Afghan
S
6

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.

Subtlety answered 9/5, 2011 at 6:13 Comment(2)
I was starting to think along those lines. It kind of makes total sense to have a single delegate for the MGTwitterEngine and get that to route results back to the correct view/view controller. Thank you so much!Afghan
I came to the same conclusion when I was working on a little project last year. I ended up implementing a subscribe:(id)target toResultsOf:(NSString)connectionId method that used a NSMutableDictionary to store subscriptions. Once the delegate receives the results, it looks up the target, forwards data to a method with the same signature in the controller, and removes the subscription.Yellowwood
L
2

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!

Litt answered 5/5, 2011 at 19:59 Comment(2)
Just wondering how that would work if the app made a few asynchronous Twitter requests at the same time from different controllers using the same twitter engine object. Essentially I can see the requests all come back to the delegate that was last registered. Should really be sent through with the request?Afghan
The response would come back to the last registred delegate... either you have to treat that in every class that is going to use the mgTwitterEngine, or you can use the singleton method, er even try to use a singleton that treats only the answers via notification center...Litt
M
0

Use NSNotifications in the delegate.

Make the view controller where you wish the delegate to be add an observer. Have the delegate method for MGTwitterEngine post the notification.

Marishamariska answered 20/7, 2012 at 1:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.