मैंने NSURLConnection को उप-करने और एक टैग, डेलीगेट और एक NSMutabaleData जोड़ने का फैसला किया। मेरे पास एक DataController क्लास है जो अनुरोधों सहित सभी डेटा प्रबंधन को संभालती है। मैंने एक DataControllerDelegate प्रोटोकॉल बनाया, ताकि व्यक्तिगत विचार / वस्तुएं DataController को सुन सकें ताकि यह पता लगाया जा सके कि उनके अनुरोध कब समाप्त हुए थे, और यदि आवश्यक हो तो कितना डाउनलोड या त्रुटियां हुई हैं। DataController वर्ग एक नया अनुरोध शुरू करने के लिए NSURLConnection उपवर्ग का उपयोग कर सकता है, और उस प्रतिनिधि को बचा सकता है जो अनुरोध समाप्त होने पर पता करने के लिए DataController को सुनना चाहता है। यह XCode 4.5.2 और ios 6 में मेरा काम करने वाला समाधान है।
DataController.h फ़ाइल जो DataControllerDelegate प्रोटोकॉल की घोषणा करता है)। DataController भी एक सिंगलटन है:
@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
DataController.m फ़ाइल में मुख्य विधियाँ:
-(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]];
}
और एक अनुरोध शुरू करने के लिए: [[NSURLConnectionWithDelegate alloc] initWithRequest:request delegate:self startImmediately:YES tag:@"Login" dataDelegate:delegate];
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
और 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