RestKit ActivityIndi​​cator 用于多个请求

发布于 2024-12-15 09:13:19 字数 279 浏览 2 评论 0原文

我有一个 IBAction,可以通过 iPad 应用程序中的按钮调用。

IBaction 然后调用三个不同的方法来发起 RestKit 请求。

检查所有三个请求检索的对象的最佳方法是什么?

目标是通过第一个请求显示 ActivityIndi​​cator,并通过检索所有三个请求的对象来停止/隐藏 ActivityIndi​​cator。

我是 RestKit 的新手,所以如果这是一个太简单的问题,请原谅我。我尝试在 RestKit API - 文档的帮助下自己找到解决方案。

I have a IBAction which is called via a button in my iPad App.

The IBaction Calls then three different methods which initiate a RestKit Request.

What would be the best way to check of for all three requests the objects where retrieved?

The goal is to show an activityIndicator with the first request and stop/hide the activityIndicator with the retrieval of objects for all three requests.

I'm new to RestKit, so please forgive me if this is a too simple question. I tried to find a solution by myself with the help of the RestKit API - Documentation.

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

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

发布评论

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

评论(2

陌伤浅笑 2024-12-22 09:13:19

我想你使用异步请求。一种简单的解决方案是检查 RKObjectLoderDelegate- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects 方法是否包含所有数据已经加载。

启动 RestKit 请求时,您可以向请求添加一个“标签”(在 RestKit 中称为 userData),并稍后在委托回调中检索该标签。

例如,您可以实现以下逻辑:

当您创建请求时,向每个请求添加特定的用户数据:

RKObjectLoader *loader = [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/getMyData" objectMapping:mapping delegate:self];     
[loader setUserData:@"FirstRequest"];
...
RKObjectLoader *loader = [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/getMyData" objectMapping:mapping delegate:self];     
[loader setUserData:@"ThirdRequest"];

并检查 didLoadObjects: 中的标签:

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
    if ([[objectLoader userData] isEqual:@"FirstRequest"]) {
        self.firstRequestData = objects;
    }
    ... //handle the remaining requests
    if (self.firstRequestData && self.secondRequestData && self.thirdRequestData) {
        [self hideMyActivityIndicator];
    }   
 }

加载请求时不要忘记处理错误情况请求。

或者,如果此方法不适合您的场景 - 您可以在单独的队列和队列中添加和管理请求。跟踪进度。如果您需要有关第二个选项的更多信息,请告诉我。

I suppose you use asynchronous requests. One simple solution can be a check in your RKObjectLoderDelegate's - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects method whether all of your data is already loaded.

When initiating RestKit requests, you can add a 'tag' (called userData in RestKit) to a request and retrieve that tag later in the delegate callbacks.

For example, you can implement the following logic:

When you create your requests, add a specific user data to each one:

RKObjectLoader *loader = [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/getMyData" objectMapping:mapping delegate:self];     
[loader setUserData:@"FirstRequest"];
...
RKObjectLoader *loader = [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/getMyData" objectMapping:mapping delegate:self];     
[loader setUserData:@"ThirdRequest"];

and check the tags in didLoadObjects:

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
    if ([[objectLoader userData] isEqual:@"FirstRequest"]) {
        self.firstRequestData = objects;
    }
    ... //handle the remaining requests
    if (self.firstRequestData && self.secondRequestData && self.thirdRequestData) {
        [self hideMyActivityIndicator];
    }   
 }

Don't forget to handle error situations when loading the requests.

Alternatively, if this approach will not fit your scenario - you can add and manage requests in a separate queue & track the progress. If you'll need more info on the second option just let me know.

梨涡少年 2024-12-22 09:13:19

有一个简单的方法,在您的 objectLoader 委托中,您可以使用 objectLoader 的内置函数,如下所示:

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

    if ([objectLoader wasSentToResourcePath:@"/path1"]) {
       //handle request for /path1  
       //objects are loaded for request1        

    } else if ([objectLoader wasSentToResourcePath:@"/path2"]){
       //handle request for /path2
       //objects are loaded for request2      

    } else if ([objectLoader wasSentToResourcePath:@"/path3"]){
      //handle request for /path3
      //objects are loaded for request3      

   }
}

就这么简单。

There is an easy way, in your objectLoader delegate, you can use objectLoader's the built-in function like this:

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

    if ([objectLoader wasSentToResourcePath:@"/path1"]) {
       //handle request for /path1  
       //objects are loaded for request1        

    } else if ([objectLoader wasSentToResourcePath:@"/path2"]){
       //handle request for /path2
       //objects are loaded for request2      

    } else if ([objectLoader wasSentToResourcePath:@"/path3"]){
      //handle request for /path3
      //objects are loaded for request3      

   }
}

Its just that simple.

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