使用 MGTwitterEngine 单例

发布于 2024-11-08 12:22:38 字数 676 浏览 0 评论 0原文

我读到了更改 MGTwitterEngine 的委托,但并没有真正理解它。我希望有人能再次解释一下。

根据我所知,我为 MGTwitterEngine 创建一个包装器,并在包装​​器中设置委托。因此,为了变得更容易,我尝试为接口提供一个 NSArray 实例,每当需要时我都会传递它。

下面是接收到的状态的代码:

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

    [statusIds setObject:statuses forKey:connectionIdentifier];
}

所以我希望项目中的任何对象都可以访问sharedTwitterEngine,只要我先请求信息,然后释放statusContainer,使用新的结果并将其传递给我的工作对象以供稍后使用使用。

我不确定这是否是正确的方法,或者是否有我错过的更简单的方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2024-11-15 12:22:38

您链接的 SO 帖子提出的解决方案可以通过以下方式实现:

1)创建 MGTwitterEngine 的包装器;这个包装器将公开 MGTwitterEngine 所需的任何选择器,并向每个选择器添加一个参数,该参数标识正在调用它的视图控制器;

2) MGTwitterEngine 的包装器将充当所有发送请求的唯一委托;

3) 对于包装器从视图控制器接收到的每个请求,包装器会将视图控制器地址存储在与 twitter id 关联的 NSMutableDictionary 中;

4) 当响应返回时,委托(与包装器是同一对象)将找出最初发送请求的视图控制器(通过在字典中搜索响应附带的 twitter id),并转发对此作出回应。

我希望这会有所帮助......

编辑:

这就是你可以做到的(我只包括 1 个 API 调用和相关代码):

@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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文