Search code examples
iphonemgtwitterengine

Using MGTwitterEngine Singleton


I read about Change the delegate of MGTwitterEngine and don't really get it. I hope someone can explain it again.

Based on what I know, I create a wrapper for MGTwitterEngine and setup the delegate within the wrapper. So in order to make easier, I try to have an NSArray instance for the interface which I'll pass around whenever I need it.

Here is the code for the status received:

- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
{
    //NSLog(@"Got statuses for %@:\r%@", connectionIdentifier, statuses);

    [statusIds setObject:statuses forKey:connectionIdentifier];
}

So I expect the sharedTwitterEngine can be accessed by any object within the project, as long I request for information first, and release the statusContainer, using the new result and pass it to my working object for later use.

I'm not sure if it is the correct way, or are there any easier ways that I missed?


Solution

  • The solution proposed by the S.O. post you link could be implemented in this way:

    1) create a wrapper to MGTwitterEngine; this wrapper will expose any selector you need of MGTwitterEngine and will add to each of them a parameter which identifies the view controller that is calling it;

    2) your wrapper to MGTwitterEngine would act as a unique delegate to all requests sent;

    3) for each request that the wrapper receives from a view controller, the wrapper will store the view controller address in a NSMutableDictionary associated to the twitter id;

    4) when a response comes back, the delegate (which is the same object as the wrapper) will find out which view controller sent the request initially (by searching in the dictionary for the twitter id that came with the response), and forward the response to it.

    I hope this helps....

    EDIT:

    this is how you could do it (I am including only 1 API call and just the relevant code):

    @interface TwitterClientViewController : UIViewController <MGTwitterEngineDelegate> {
    }
    @end
    
    @implementation TwitterClientViewController;
    
    - (void)requestListOfUsers:(NSString*)username {
        [twitterEngineSingleton getListsForUser:username requestDelegate:self];
    }
    
    - (void)requestSucceeded:(NSString*)connectionIdentifier {
        NSLog(@"Hello");
    }
    @end
    
    @interface AdvancedTwitterEngine : NSObject <MGTwitterEngineDelegate> {
        MGTwitterEngine* _engine;
        NSMutableDictionary* _callerIds;
    }
    -(NSString*)getListsForUser:(NSString*)username requestDelegate:(id<MGTwitterEngineDelegate>)delegate;
    @end
    
    @implementation AdvancedTwitterEngine;
    
    -(void)init {
        if (self = [super init]) {
            _engine = [[MGTTwitterEngine alloc] initWithDelegate:self];
            _callerIds = <init>
        }
        return self;
    }
    
    -(NSString*)getListsForUser:(NSString*)username requestDelegate:(id<MGTwitterEngineDelegate>)delegate {
        NSString* twId = [_engine getListsForUser:username];
        [_callerIds setObject:controller forKey:twId];
        return twId;
    }
    
    //-- delegate methods
    
    - (void)requestSucceeded:(NSString*)connectionIdentifier {
        id<MGTwitterEngineDelegate> dlg = [_callerIds objectForKey:connectionIdentifier];
        [dlg requestSucceeded:connectionIdentifier];
    }
    
    @end