多个异步Web服务请求NSURLConnection iOS [英] Multiple async webservice requests NSURLConnection iOS

查看:126
本文介绍了多个异步Web服务请求NSURLConnection iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我不知道如何解决。在传递单个请求时,我可以毫无问题地向REST服务发出请求。

I have a problem that Im not sure how to tackle. I can without any problem make requests to the REST service when passing a single request.

我现在的问题是基于该响应我得到一些值,包括ID。然后,检索到的所有ID都需要另外请求收集新信息。

My problem now is at based on that respons i get some values including ID´s. All ID´s retrieved will then need make another request to collect new information.

我的猜测是在数组或字典中加载所有特定请求并从中创建请求。有人有一些有用的提示吗?然后,检索到的信息将填充UITableView。

My guess is to load all the specific request in a array or dictionary and create request from that. Anyone have some useful tips regarding this? The information retrieved will then populate a UITableView.

推荐答案

我建议你在这个问题上使用同步 - 异步模式。

I suggest you use a Sync-Async Pattern on this problem.

您需要实现两个同步方法:

You need to implement two synchronous methods:

// Fetch the array of ID's
-(NSArray*)fetchItemIDsWithError:(NSError**)error;

// Fetch the item for a ID
-(Item*)fetchItemForID:(NSString*)itemID error:(NSError**)error;

使用同步代码实现这些操作非常简单且易于测试。您可以使用简单的方法,如 dataWithURL ... stringWithContentsOfURL ... sendSynchronousRequest ... ,或者ASIHTTPrequest轻松,并为此编写直接的单元测试。与并发代码通常结束的方式相比,代码也将非常易于维护和扩展。

Implementing these using synchronous code is easy and testable. You can use simple methods like dataWithURL…, stringWithContentsOfURL…, sendSynchronousRequest…, or ASIHTTPrequest with ease, and write straight forward unit tests for this. The code will also be extremely easy to maintain and extend, compare to how concurrent code usually ends up.

现在到第二步,创建一个异步包装器,我会使用一个委托和一个这样的方法签名:

Now to step two, create an asynchronous wrapper, I would use a delegate and a method signature like this:

@protocol FetchItemsDelegate <NSObject>
-(void)didFetchItems:(NSArray*)array;
-(void)failedFetchItemsWithError:(NSError*)error;
@end

-(void)fetchItemsWithAsyncDelegate:(id<FetchItemsDelegate>)delegate;

您已经拥有了所需的所有代码,所以您所要做的就是阻止异步部分。这段代码将很好地分离和直接。 Probaly不超过这个:

You already have all the code that do what you need, so all you have to do is impelemnt the asynchronious parts. This code will be well sepearated and straight forward. Probaly not more than this:

-(void)fetchItemsWithAsyncDelegate:(id<FetchItemsDelegate>)delegate;
{
    [self performSelectorInBackground:@selector(backgroundFetchItemsWithDelegate:)
                           withObject:delegate];      
}

-(void)backgroundFetchItemsWithDelegate:(id<FetchItemsDelegate>)delegate;
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    BOOL success = YES;
    NSMutableArray* items = [NSMutableArray array];
    NSError* error = nil;
    NSArray* itemIDs = [self fetchItemIDsWithError:&error];
    if (itemIDs) {
        for (NSString* itemID in itemIDs) {
           Item* item = [self fetchItemForID:itemID
                                       error:&error];
           if (item) {
               [items addObject:item];
           } else {
               success = NO;
               break;
           }
        }
    } else {
        success = NO;
    }
    if (success) {
        [delegate performSelectorOnMainThread:@selector(didFetchItems:)
                                   withObject:[NSArray arraiWithArray:items]
                                waitUntilDone:NO];
    } else {
        [delegate performSelectorOnMainThread:@selector(failedFetchItemsWithError)
                                   withObject:error
                                waitUntilDone:NO];
    }
    [pool release];
}

我在这里写了一篇关于这个主题的博文: http://blog.jayway.com/ 2011/04/28 / sync-asyn-pair-pattern-easy-concurrency-on-ios /

I have written a longer blog post on this topic here: http://blog.jayway.com/2011/04/28/sync-asyn-pair-pattern-easy-concurrency-on-ios/

这篇关于多个异步Web服务请求NSURLConnection iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆