Multiple asynchronous webservice NSURLConnection iOS requests

I have a problem that I'm not sure how to fight. I can send requests to the REST service without any problems when sending a single request.

Now my problem is based on the fact that I get some values, including identifiers. Then, to obtain all identifiers, another request will be required to collect new information.

My guess is to load the entire specific query into an array or dictionary and create a query from it. Does anyone have any helpful tips on this? The resulting information will then populate the UITableView.

+5
source share
3 answers

Sync-Async .

:

// 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, . , , .

, , , :

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

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

, , , , , impelemnt . . :

-(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/

+4

, , . http-:

@protocol HttpDelegate
   -(void) httpDidFinish;
   -(void) httpError:(NSError *) error;
@end

HttpClient:

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
    [self.delegate httpDidFinish];
}

( ) HttpDelegate, httpDidFinish , . , , HttpDelegate , : TwoStepProcessor TwoStepProcessorDelegate. TwoStepProcessorDelegate HttpDelegate, , :

-(void) secondStepFinished:

.

0

( .)

I: If you are having a different class for managing connection related task, a separate class that has NSURLConnection Delegate methods.(Asynchronous..)

  • for 20 ID . , , , , , . [ ]... , , ( )

II: If its singleton or in the same class which you are using.(we can not create it multiple objects)..(it will have the performance cost.) one by one request.

  • You will need to send the request one by one and update the cell contents before it is completed. This means that you send a request for identifier 1, and when its response arrives, you can update the cell and send the next request. etc.

It will be useful if you tell us how you handle connection activity.

Thank,

0
source

All Articles