Queue for managing network requests
STNetTaskQueue may be your choice if you want to handle each network request stuff in separated STNetTask instead of having all the network requests logics in a "Manager" class.
STHTTPNetTaskQueueHandler is included, which is for HTTP based network reqeust. If you are looking for a socket or other protocol based handler, currently you should write your own net task queue handler and conform to STNetTaskQueueHandler protocol. STHTTPNetTaskQeueuHandler depends on AFNetworking, which is included in example project.
- Retry net task with specified max retry count.
- Delegate for net task result according to "uri" of net task.
- More features are coming as STNetTaskQueue is extendible.
CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like AFNetworking in your projects. See the "Getting Started" guide for more information.
platform :ios, '7.0'
pod 'STNetTaskQueue', '~> 0.0.1'NSURL *baseUrl = [NSURL URLWithString:@"http://api.openweathermap.org"];
STHTTPNetTaskQueueHandler *httpHandler = [[STHTTPNetTaskQueueHandler alloc] initWithBaseURL:baseUrl];
[STNetTaskQueue sharedQueue].handler = httpHandler;@interface STOpenWeatherNetTask : STHTTPNetTask
@property (nonatomic, strong) NSString *latitude;
@property (nonatomic, strong) NSString *longitude;
@property (nonatomic, strong, readonly) NSString *place;
@property (nonatomic, assign, readonly) float temperature;
@end@implementation STOpenWeatherNetTask
- (STHTTPNetTaskMethod)method
{
return STHTTPNetTaskGet;
}
- (NSString *)uri
{
return @"data/2.5/weather";
}
- (NSUInteger)maxRetryCount
{
return 3;
}
- (NSDictionary *)parameters
{
return @{ @"lat": self.latitude,
@"lon": self.longitude };
}
- (void)didResponseJSON:(NSDictionary *)response
{
_place = response[@"name"];
_temperature = [response[@"main"][@"temp"] floatValue] / 10;
}
@endif (_openWeatherTask.pending) {
return;
}
_openWeatherTask = [STOpenWeatherNetTask new];
_openWeatherTask.latitude = @"1.306038";
_openWeatherTask.longitude = @"103.772962";
// Task delegate will be a weak reference, so no need to remove it manually.
// Duplicated task delegates will be ignored by STNetTaskQueue, so it's fine to invoke addTaskDelegate here.
[[STNetTaskQueue sharedQueue] addTaskDelegate:self uri:_openWeatherTask.uri];
[[STNetTaskQueue sharedQueue] addTask:_openWeatherTask];- (void)netTaskDidEnd:(STNetTask *)task
{
// It's necessary to detect if _openWeatherTask != task and return,
// if you have mutiple instance/viewController deleagating the same uri.
if (_openWeatherTask != task) {
return;
}
if (task.error) { // Would be network issue
_resultLabel.text = @"Network Unavailable";
_goBtn.hidden = YES;
return;
}
_resultLabel.text = [NSString stringWithFormat:@"%@\n%.1f°C", _openWeatherTask.place, _openWeatherTask.temperature];
_goBtn.hidden = YES;
}You can see more details in example project. The example is tested with iOS SDK 8.1, XCode 6.1.1 and iPhone 6 simulator.
