Managing multiple asynchronous NSURLConnection connections
Asked Answered
D

13

88

I have a ton of repeating code in my class that looks like the following:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                              delegate:self];

The problem with asynchronous requests is when you have various requests going off, and you have a delegate assigned to treat them all as one entity, a lot of branching and ugly code begins to formulate going:

What kind of data are we getting back? If it contains this, do that, else do other. It would be useful I think to be able to tag these asynchronous requests, kind of like you're able to tag views with IDs.

I was curious what strategy is most efficient for managing a class that handles multiple asynchronous requests.

Delamination answered 1/12, 2008 at 21:21 Comment(0)
C
77

I track responses in an CFMutableDictionaryRef keyed by the NSURLConnection associated with it. i.e.:

connectionToInfoMapping =
    CFDictionaryCreateMutable(
        kCFAllocatorDefault,
        0,
        &kCFTypeDictionaryKeyCallBacks,
        &kCFTypeDictionaryValueCallBacks);

It may seem odd to use this instead of NSMutableDictionary but I do it because this CFDictionary only retains its keys (the NSURLConnection) whereas NSDictionary copies its keys (and NSURLConnection doesn't support copying).

Once that's done:

CFDictionaryAddValue(
    connectionToInfoMapping,
    connection,
    [NSMutableDictionary
        dictionaryWithObject:[NSMutableData data]
        forKey:@"receivedData"]);

and now I have an "info" dictionary of data for each connection that I can use to track information about the connection and the "info" dictionary already contains a mutable data object that I can use to store the reply data as it comes in.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSMutableDictionary *connectionInfo =
        CFDictionaryGetValue(connectionToInfoMapping, connection);
    [[connectionInfo objectForKey:@"receivedData"] appendData:data];
}
Chinaman answered 1/12, 2008 at 22:37 Comment(9)
Since it is possible that two or more asynchronous connections may enter the delegate methods at a time, is there anything specific that one would need to do to ensure correct behavior?Elgon
(I have create a new question here asking this: #1192794 )Elgon
This is not thread safe if the delegate is being called from multiple threads. You must use mutual exclusion locks to protect the data structures. A better solution is subclassing NSURLConnection and adding response and data references as instance variables. I am providing a more detailed answer explaining this at Nocturne's question: #1192794Patency
As stated before, this is not thread safe. I'd rather prefer to solve this issue by using NSOperation subclasses and NSOperationQueue, which in addion allows you to manage the number of ongoing concurrent requests at one time. Each of these operations may handle their NSURLConnection callbacks and inform their individual delegates about progress and results.Modestia
Aldi... it is thread safe provided you start all connections from the same thread (which you can do easily by invoking your start connection method using performSelector:onThread:withObject:waitUntilDone:). Putting all connections in an NSOperationQueue has different problems if you try to start more connections than the max concurrent operations of the queue (operations get queued instead of running concurrently). NSOperationQueue works well for CPU bound operations but for network bound operations, you're better off using an approach that doesn't use a fixed size thread pool.Chinaman
This is really useful - thanks! How do I get rid of this warning? "warning: passing argument 1 of 'CFDictionaryAddValue' discards qualifiers from pointer target type"Thousandfold
This really helped as well! And @josh-brown , one reason you might be getting this warning is if you are using CFMutableDictionaryRef as a pointer... I mean CFMutableDictionaryRef *dic = CFDictionaryCreateMutable(... instead of just CFMutableDictionaryRef dic = CFDictionaryCreateMutable(...Oneness
Another method for doing this is shown here: github.com/dbowen/Demiurgic-JSON-RPC. @Matt Gallagher, what are the pros and cons of this method versus your own solution?Graecize
Just wanted to share that for iOS 6.0 and above, you can use a [NSMapTable weakToStrongObjectsMapTable] instead of a CFMutableDictionaryRef and save the hassle. Worked well for me.Hydra
B
19

I have a project where I have two distinct NSURLConnections, and wanted to use the same delegate. What I did was create two properties in my class, one for each connection. Then in the delegate method, I check to see if which connection it is


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (connection == self.savingConnection) {
        [self.savingReturnedData appendData:data];
    }
    else {
        [self.sharingReturnedData appendData:data];
    }
}

This also allows me to cancel a specific connection by name when needed.

Buke answered 24/1, 2011 at 20:18 Comment(6)
be careful this is problematic as it will have race conditionsDeuteranope
How do you assign the names (savingConnection and sharingReturnedData) for each connection in the first place?Charlyncharm
@adit, no, there is no race condition inherent to this code. You'd have to go pretty far out of your way with the connection creation code to create a race conditionCasilde
your 'solution' is exactly what the original question is looking to avoid, quoting from above: '... a lot of branching and ugly code begins to formulate ...'Folks
@Deuteranope Why will this lead to a race condition? It's a new concept to me.Karrykarst
I'm new in iOS/Objective-C. I tried using this kind of approach for my NSURLConnections and doesn't work when used in if-elseHedjaz
B
16

Subclassing NSURLConnection to hold the data is clean, less code than some of the other answers, is more flexible, and requires less thought about reference management.

// DataURLConnection.h
#import <Foundation/Foundation.h>
@interface DataURLConnection : NSURLConnection
@property(nonatomic, strong) NSMutableData *data;
@end

// DataURLConnection.m
#import "DataURLConnection.h"
@implementation DataURLConnection
@synthesize data;
@end

Use it as you would NSURLConnection and accumulate the data in its data property:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    ((DataURLConnection *)connection).data = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [((DataURLConnection *)connection).data appendData:data];
}

That's it.

If you want to go further you can add a block to serve as a callback with just a couple more lines of code:

// Add to DataURLConnection.h/.m
@property(nonatomic, copy) void (^onComplete)();

Set it like this:

DataURLConnection *con = [[DataURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
con.onComplete = ^{
    [self myMethod:con];
};
[con start];

and invoke it when loading is finished like this:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    ((DataURLConnection *)connection).onComplete();
}

You can extend the block to accept parameters or just pass the DataURLConnection as an argument to the method that needs it within the no-args block as shown

Beowulf answered 17/12, 2012 at 19:25 Comment(1)
This is a fantastic answer that worked really well for my case. Very simple and clean!Stpierre
C
8

THIS IS NOT A NEW ANSWER. PLEASE LET ME SHOW YOU HOW I DID

To distinguish different NSURLConnection within same class's delegate methods, I use NSMutableDictionary, to set and remove the NSURLConnection, using its (NSString *)description as key.

The object I chose for setObject:forKey is the unique URL that is used for initiating NSURLRequest, the NSURLConnection uses.

Once set NSURLConnection is evaluated at

-(void)connectionDidFinishLoading:(NSURLConnection *)connection, it can be removed from the dictionary.

// This variable must be able to be referenced from - (void)connectionDidFinishLoading:(NSURLConnection *)connection
NSMutableDictionary *connDictGET = [[NSMutableDictionary alloc] init];
//...//

// You can use any object that can be referenced from - (void)connectionDidFinishLoading:(NSURLConnection *)connection
[connDictGET setObject:anyObjectThatCanBeReferencedFrom forKey:[aConnectionInstanceJustInitiated description]];
//...//

// At the delegate method, evaluate if the passed connection is the specific one which needs to be handled differently
if ([[connDictGET objectForKey:[connection description]] isEqual:anyObjectThatCanBeReferencedFrom]) {
// Do specific work for connection //

}
//...//

// When the connection is no longer needed, use (NSString *)description as key to remove object
[connDictGET removeObjectForKey:[connection description]];
Covenanter answered 6/7, 2010 at 22:44 Comment(0)
E
5

One approach I've taken is to not use the same object as the delegate for each connection. Instead, I create a new instance of my parsing class for each connection that is fired off and set the delegate to that instance.

Estray answered 2/12, 2008 at 5:35 Comment(1)
Much better encapsulation with respect to one connection.Tefillin
F
4

Try my custom class, MultipleDownload, which handles all these for you.

Filose answered 2/12, 2008 at 8:51 Comment(1)
on iOS6 can't use the NSURLConnection as the key.Rochet
H
2

I usually create an array of dictionaries. Each dictionary has a bit of identifying information, an NSMutableData object to store the response, and the connection itself. When a connection delegate method fires, I look up the connection's dictionary and handle it accordingly.

Henrieta answered 1/12, 2008 at 21:38 Comment(3)
Ben, would it be okay to ask you for a piece of sample code? I'm trying to envision how you're doing it, but it's not all there.Delamination
In particular Ben, how do you look up the dictionary? You can't have a dictionary of dictionaries since NSURLConnection doesn't implement NSCopying (so it can't be used as a key).Petrol
Matt has an excellent solution below using CFMutableDictionary, but I use an array of dictionaries. A lookup requires an iteration. Its not the most efficient, but it's fast enough.Henrieta
C
2

One option is just to subclass NSURLConnection yourself and add a -tag or similar method. The design of NSURLConnection is intentionally very bare bones so this is perfectly acceptable.

Or perhaps you could create a MyURLConnectionController class that is responsible for creating and collecting a connection's data. It would then only have to inform your main controller object once loading is finished.

Casilde answered 2/12, 2008 at 0:20 Comment(0)
V
2

in iOS5 and above you can just use the class method sendAsynchronousRequest:queue:completionHandler:

No need to keep track of connections since the response returns in the completion handler.

Vitriform answered 13/12, 2012 at 0:6 Comment(0)
H
1

As pointed out by other answers, you should store connectionInfo somewhere and look up them by connection.

The most natural datatype for this is NSMutableDictionary, but it cannot accept NSURLConnection as keys as connections are non copyable.

Another option for using NSURLConnections as keys in NSMutableDictionary is using NSValue valueWithNonretainedObject]:

NSMutableDictionary* dict = [NSMutableDictionary dictionary];
NSValue *key = [NSValue valueWithNonretainedObject:aConnection]
/* store: */
[dict setObject:connInfo forKey:key];
/* lookup: */
[dict objectForKey:key];
Hypozeuxis answered 7/2, 2009 at 15:15 Comment(0)
L
1

I like ASIHTTPRequest.

Leucocyte answered 26/5, 2010 at 11:24 Comment(1)
I really like the 'blocks' implementation in ASIHTTPRequest - it's just like Anonymous Inner Types in Java. This beats all the other solutions in terms of code cleanliness and organisation.Calculator
C
0

I decided to subclass NSURLConnection and add a tag, delegate, and a NSMutabaleData. I have a DataController class that handles all of the data management, including the requests. I created a DataControllerDelegate protocol, so that individual views/objects can listen to the DataController to find out when their requests were finished, and if needed how much has been downloaded or errors. The DataController class can use the NSURLConnection subclass to start a new request, and save the delegate that wants to listen to the DataController to know when the request has finished. This is my working solution in XCode 4.5.2 and ios 6.

The DataController.h file that declares the DataControllerDelegate protocol). The DataController is also a singleton:

@interface DataController : NSObject

@property (strong, nonatomic)NSManagedObjectContext *context;
@property (strong, nonatomic)NSString *accessToken;

+(DataController *)sharedDataController;

-(void)generateAccessTokenWith:(NSString *)email password:(NSString *)password delegate:(id)delegate;

@end

@protocol DataControllerDelegate <NSObject>

-(void)dataFailedtoLoadWithMessage:(NSString *)message;
-(void)dataFinishedLoading;

@end

The key methods in the DataController.m file:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSURLConnectionWithDelegate *customConnection = (NSURLConnectionWithDelegate *)connection;
    NSLog(@"DidReceiveResponse from %@", customConnection.tag);
    [[customConnection receivedData] setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSURLConnectionWithDelegate *customConnection = (NSURLConnectionWithDelegate *)connection;
    NSLog(@"DidReceiveData from %@", customConnection.tag);
    [customConnection.receivedData appendData:data];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSURLConnectionWithDelegate *customConnection = (NSURLConnectionWithDelegate *)connection;
    NSLog(@"connectionDidFinishLoading from %@", customConnection.tag);
    NSLog(@"Data: %@", customConnection.receivedData);
    [customConnection.dataDelegate dataFinishedLoading];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSURLConnectionWithDelegate *customConnection = (NSURLConnectionWithDelegate *)connection;
    NSLog(@"DidFailWithError with %@", customConnection.tag);
    NSLog(@"Error: %@", [error localizedDescription]);
    [customConnection.dataDelegate dataFailedtoLoadWithMessage:[error localizedDescription]];
}

And to start a request: [[NSURLConnectionWithDelegate alloc] initWithRequest:request delegate:self startImmediately:YES tag:@"Login" dataDelegate:delegate];

The NSURLConnectionWithDelegate.h: @protocol DataControllerDelegate;

@interface NSURLConnectionWithDelegate : NSURLConnection

@property (strong, nonatomic) NSString *tag;
@property id <DataControllerDelegate> dataDelegate;
@property (strong, nonatomic) NSMutableData *receivedData;

-(id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately tag:(NSString *)tag dataDelegate:(id)dataDelegate;

@end

And the NSURLConnectionWithDelegate.m:

#import "NSURLConnectionWithDelegate.h"

@implementation NSURLConnectionWithDelegate

-(id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately tag:(NSString *)tag dataDelegate:(id)dataDelegate {
    self = [super initWithRequest:request delegate:delegate startImmediately:startImmediately];
    if (self) {
        self.tag = tag;
        self.dataDelegate = dataDelegate;
        self.receivedData = [[NSMutableData alloc] init];
    }
    return self;
}

@end
Checky answered 14/11, 2012 at 0:44 Comment(0)
M
0

Every NSURLConnection has an hash attribute, you can discriminate all by this attribute.

For example i need to mantain certain information before and after connection, so my RequestManager have an NSMutableDictionary to do this.

An Example:

// Make Request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *c = [[NSURLConnection alloc] initWithRequest:request delegate:self];

// Append Stuffs 
NSMutableDictionary *myStuff = [[NSMutableDictionary alloc] init];
[myStuff setObject:@"obj" forKey:@"key"];
NSNumber *connectionKey = [NSNumber numberWithInt:c.hash];

[connectionDatas setObject:myStuff forKey:connectionKey];

[c start];

After request:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Received %d bytes of data",[responseData length]);

    NSNumber *connectionKey = [NSNumber numberWithInt:connection.hash];

    NSMutableDictionary *myStuff = [[connectionDatas objectForKey:connectionKey]mutableCopy];
    [connectionDatas removeObjectForKey:connectionKey];
}
Microminiaturization answered 23/12, 2013 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.