public
Last active

An example showing how to manage a queue with ASIHTTPRequest

  • Download Gist
gistfile1.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
//
// MyController.h
//
// Created by Ben Copsey on 20/07/2009.
// Copyright 2009 All-Seeing Interactive. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import <GHUnit/GHUnit.h>
@class ASINetworkQueue;
 
@interface MyController : NSObject {
ASINetworkQueue *networkQueue;
}
 
- (void)doNetworkOperations;
 
@property (retain) ASINetworkQueue *networkQueue;
 
@end
 
-------------------------------------------------------------------
 
//
// MyController.m
//
// Created by Ben Copsey on 20/07/2009.
// Copyright 2009 All-Seeing Interactive. All rights reserved.
//
 
#import "MyController.h"
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
 
@implementation MyController
 
- (void)dealloc
{
[networkQueue release];
[super dealloc];
}
 
- (void)doNetworkOperations
{
// Stop anything already in the queue before removing it
[[self networkQueue] cancelAllOperations];
// Creating a new queue each time we use it means we don't have to worry about clearing delegates or resetting progress tracking
[self setNetworkQueue:[ASINetworkQueue queue]];
[[self networkQueue] setDelegate:self];
[[self networkQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
[[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
[[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];
 
int i;
for (i=0; i<5; i++) {
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]];
[[self networkQueue] addOperation:request];
}
[[self networkQueue] go];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// You could release the queue here if you wanted
if ([[self networkQueue] requestsCount] == 0) {
 
// Since this is a retained property, setting it to nil will release it
// This is the safest way to handle releasing things - most of the time you only ever need to release in your accessors
// And if you an Objective-C 2.0 property for the queue (as in this example) the accessor is generated automatically for you
[self setNetworkQueue:nil];
}
//... Handle success
NSLog(@"Request finished");
}
 
- (void)requestFailed:(ASIHTTPRequest *)request
{
// You could release the queue here if you wanted
if ([[self networkQueue] requestsCount] == 0) {
[self setNetworkQueue:nil];
}
//... Handle failure
NSLog(@"Request failed");
}
 
 
- (void)queueFinished:(ASINetworkQueue *)queue
{
// You could release the queue here if you wanted
if ([[self networkQueue] requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(@"Queue finished");
}
 
@synthesize networkQueue;
@end

Please help, I am not sure how to use it. Thanks

there are lots and lots of operations and i dont want to call [networkQueue go]; in the end... instead i want the operations to continue as i keep adding more to the queue???

How would one add a custom accurate progress indicator to this code? I have gone through the docs but cannot figure it out.

Zeokat thanks for the code i go to play with this.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.