RestKit:区分didLoadResponse中的多个请求:

发布于 2024-12-19 17:35:33 字数 492 浏览 0 评论 0原文

我想使用 RestKit 并在同一类中处理多个不同的请求,即在 didLoadResponse: 方法中。如何区分不同的请求?我如何知道哪个请求已完成?

执行请求

RKClient *client = [RKClient sharedClient];
[client get:@"/....", method] delegate:self];

我正在通过然后,在委托方法中

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    if (???) // request which gets XY returned
        ...
    else if (???) // request which gets YZ returned
        ...
}

,这可能吗?

I'd like to use RestKit and handle several different requests in the same class, i.e. in the didLoadResponse: method. How can I distinguish between the different requests? How do I know which request is finished?

I'm doing the request via

RKClient *client = [RKClient sharedClient];
[client get:@"/....", method] delegate:self];

Then, in the delegate-method

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    if (???) // request which gets XY returned
        ...
    else if (???) // request which gets YZ returned
        ...
}

is that possible?

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

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

发布评论

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

评论(2

沫离伤花 2024-12-26 17:35:33

当然,RKClient get: 方法返回一个 RKRequest 对象。只需将 userData 设置为请求并稍后在委托中检索它即可。

RKClient *client = [RKClient sharedClient];
RKRequest *request = [client get:@"/....", method] delegate:self];
[request setUserData:@"FirstRequest"];

稍后在委托中检查它

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    id userData = [request userData];
    if ([userData isEqual:@"FirstRequest"]) // request which gets XY returned
        ...
    else if (...) // request which gets YZ returned
        ...
}

Sure, the RKClient get: method returns a RKRequest object. Just set a userData to the request and retrieve it later in the delegate.

RKClient *client = [RKClient sharedClient];
RKRequest *request = [client get:@"/....", method] delegate:self];
[request setUserData:@"FirstRequest"];

and check it later in the delegate

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    id userData = [request userData];
    if ([userData isEqual:@"FirstRequest"]) // request which gets XY returned
        ...
    else if (...) // request which gets YZ returned
        ...
}
呆萌少年 2024-12-26 17:35:33

这不是您问题的确切答案,但我感觉有些人会像我一样来到这里想知道如何区分 didLoadObjects 中的多个请求。解决方案是使用 isKindOfClass。

例如,当用户登录我的应用程序时,我会进行两次 HTTP 调用,并且我想区分从 getUser 调用返回的对象与 getSummary 返回的对象(因为如果我不这样做,那么它就会崩溃)。此代码检查返回的对象是否是该特定类的“某种”,如果是,则将该对象设置为该对象的本地实例。

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {

    if ([[objects objectAtIndex:0] isKindOfClass:[APIUser class]]) {

        APIUser *apiUser = [objects objectAtIndex:0];

    }
    else if ([[objects objectAtIndex:0] isKindOfClass:[APIUserSummary class]]) {

        APIUserSummary *summary = [objects objectAtIndex:0];

    }
}

This isn't an exact answer to your question, but I have the feeling that some people will come here wondering how to distinguish multiple requests in didLoadObjects, as I did. The solution is to use isKindOfClass.

For example, I make two HTTP calls when a user logs into my app, and I want to distinguish the object returned from the getUser call from the object returned by getSummary (because if I don't then it crashes). This code checks if the returned object is a "kind of" that particular class, and if so sets the object to a local instance of that object.

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {

    if ([[objects objectAtIndex:0] isKindOfClass:[APIUser class]]) {

        APIUser *apiUser = [objects objectAtIndex:0];

    }
    else if ([[objects objectAtIndex:0] isKindOfClass:[APIUserSummary class]]) {

        APIUserSummary *summary = [objects objectAtIndex:0];

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