如何制作异步网络请求
NSURL *URL = [[NSURL alloc] initWithString:@"http://www.reddit.com/r/all.json"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:URL];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
FAURLConnectionDataTask *task = [[FAURLConnectionDataTask alloc] initWithRequest:request];
task.queue = queue;
[task addFinishContext:self block:^(__typeof__(self) blockSelf, FATaskFinishEvent *event) {
blockSelf.task = nil;
if ([event hasError]) {
NSLog(@"Error: %@", event.error);
} else {
FAURLConnectionDataResult *dataResult = event.result;
NSLog(@"Response: %@", dataResult.response);
NSLog(@"Data: %@", dataResult.data);
}
}];
self.task = task;
[task start];
如何在后台线程上解析异步网络请求的JSON响应数据
NSURL *URL = [[NSURL alloc] initWithString:@"http://www.reddit.com/r/all.json"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:URL];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
FAChainedTask *chainedTask = [[FAChainedTask alloc] init];
[chainedTask addTaskBlock:^id<FATask>(id lastResult) {
FAURLConnectionDataTask *task = [[FAURLConnectionDataTask alloc] initWithRequest:request];
task.queue = queue;
return task;
}];
[chainedTask addTaskBlock:^id<FATask>(id lastResult) {
FAURLConnectionDataResult *dataResult = lastResult;
return [[FABackgroundTask alloc] initWithBlock:^id(id<FATask> blockTask, NSError **error) {
return [NSJSONSerialization JSONObjectWithData:dataResult.data options:0 error:error];
}];
}];
[chainedTask addFinishContext:self block:^(__typeof__(self) blockSelf, FATaskFinishEvent *event) {
blockSelf.task = nil;
if ([event hasError]) {
NSLog(@"Error: %@", event.error);
} else {
NSLog(@"Result: %@", event.result);
}
}];
self.task = chainedTask;
[chainedTask start];
如何在失败时重试网络请求
NSURL *URL = [[NSURL alloc] initWithString:@"http://www.reddit.com/r/all.json"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:URL];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
FARetryTask *retryTask = [[FARetryTask alloc] init];
[retryTask setTaskBlock:^id<FATask>(id lastResult) {
FAURLConnectionDataTask *task = [[FAURLConnectionDataTask alloc] initWithRequest:request];
task.queue = queue;
return task;
}];
[retryTask addFinishContext:self block:^(__typeof__(self) blockSelf, FATaskFinishEvent *event) {
blockSelf.task = nil;
if ([event hasError]) {
NSLog(@"Error: %@", event.error);
} else {
FAURLConnectionDataResult *dataResult = event.result;
NSLog(@"Response: %@", dataResult.response);
NSLog(@"Data: %@", dataResult.data);
}
}];
self.task = retryTask;
[retryTask start];
如何并行执行两个网络任务并等待它们都完成响应
NSURL *googleURL = [[NSURL alloc] initWithString:@"http://www.google.com/"];
NSURL *redditURL = [[NSURL alloc] initWithString:@"http://www.reddit.com/"];
NSURLRequest *googleRequest = [[NSURLRequest alloc] initWithURL:googleURL];
NSURLRequest *redditRequest = [[NSURLRequest alloc] initWithURL:redditURL];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
FAParallelTask *parallelTask = [[FAParallelTask alloc] init];
parallelTask.allowPartialFailure = YES;
[parallelTask setKey:@"google" forTaskBlock:^id<FATask>(id lastResult) {
FAURLConnectionDataTask *task = [[FAURLConnectionDataTask alloc] initWithRequest:googleRequest];
task.queue = queue;
return task;
}];
[parallelTask setKey:@"reddit" forTaskBlock:^id<FATask>(id lastResult) {
FAURLConnectionDataTask *task = [[FAURLConnectionDataTask alloc] initWithRequest:redditRequest];
task.queue = queue;
return task;
}];
[parallelTask addFinishContext:self block:^(__typeof__(self) blockSelf, FATaskFinishEvent *event) {
blockSelf.task = nil;
NSLog(@"Results: %@", event.result);
}];
self.task = parallelTask;
[parallelTask start];
狂躁工具是一个适合iOS的复合任务框架。复合任务以更自然的方式允许编写链式或并行异步功能。这与Async.js和其他异步控制流库以及框架提供的内容类似,但优点在于对象是可组合的,而不仅仅是函数。复合任务可以生成更多不仅仅是错误和返回值的内容,还能生成事件。例如,重试任务会在任务因失败而延迟后生成事件,然后在这些任务重新启动后也生成事件。
以下是一些您可以使用复合任务实现的任务
目前,FranticApparatus可以使用两种方式使用:直接将源文件复制到您的项目中,或者将其作为静态库使用,将FranticApparatus.xcodeproj文件拖放到您的项目中或工作区。还有一个podspec文件,可以在项目的Podfile中用作本地文件引用。
FranticApparatus 1.0需要ARC和iOS 6.0以上的版本。
FranticApparatus在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。