AFNetworking 缺少哪些主要的 ASIHTTPRequest 特性?

对于 最近在 ASIHTTPRequest 上停止的工作,人们的注意力似乎正在转向 AFNetworking

然而,我还没有找到这两个库的特性的一个很好的比较,所以我不知道如果/当我切换的时候我可能会失去什么。

到目前为止,我发现的主要差异有:

  1. AFNetworking 的代码大小要小得多(这很好)
  2. AFNetworking 正在迅速改进(所以它可能还不成熟,可能还没有一个稳定的 API?)
  3. 两者似乎都有缓存,尽管我看到了一些提示,因为 AFNetworking 使用 它不会缓存超过50K 的对象
  4. ASIHTTPRequest 对手动和自动(PAC) http 代理有很好的支持; 我找不到任何关于 AFNetworking 对代理的支持级别的信息
  5. AFNetworking 需要 iOS4 + ,而 ASIHTTPRequest 可以回到 iOS2(对我来说不是问题,但对某些人来说是问题)
  6. AFNetworking 还没有一个内置的持久缓存,但是有一个持久缓存,它有一个挂起的拉请求: https://github.com/gowalla/AFNetworking/pull/25

有没有人看到这两个库之间有什么很好的比较,或者有什么文档记录了从一个库切换到另一个库的经验?

17519 次浏览

AFNetworking works with "blocks" which is more natural for me than working with delegates like ASIHTTPRequest does.

Working with blocks it's like working with Anonymous Function in javascript.

Just finishing up a project where I'm using AFNetworking instead of ASI. Have used ASI on previous projects; it's been a great help in the past.

Here's what AFNetworking is missing (as of today) that you should know about:

  • Nothing

ASI is going away. Use AF now. It's small, it works, and it's going to continue to be supported. It's also organized more logically, especially for API clients. It has a number of great classes for oft-used special cases like asynchronous loading of images in table views.

I've been using ASI* for a while now and I absolutely love the fileupload approach of ASI, and though I am excited to jump to AFNetworking, fileupload support in AfNetworking is not as easy to use compared to ASI*.

AFNetworking doesn't support clientCertificateIdentity and clientCertificates for TLS client authentication.

We can do that with the - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge method in a subclass of AFURLConnectionOperation but it's not as easy.

I loved ASIHTTPRequest and I was sad to see it go. However, the developer of ASI was right, ASIHTTPRequest has become so large and bloated that even he couldn't devote time to bring it to par with newest features of iOS and other frameworks. I moved on and now use AFNetworking.

That said, I must say that AFNetworking is much more unstable than ASIHTTP, and for the things I use it for, it needs refinement.

I often need to make HTTP requests to 100 HTTP sources before I display my results on screen, and I have put AFHTTPNetworkOperation into an operation queue. Before all results are downloaded, I want to be able to cancel all operations inside the operation queue and then dismiss the view controller that holds the results.

That doesn't always work.

I get crashes at random times with AFNetworking, while with ASIHTTPRequest, this operations were working flawlessly. I wish I could say which specific part of AFNetworking is crashing, as it keeps crashing at different points (however, most of these times the debugger points to the NSRunLoop that creates an NSURLConnection object). So, AFNetworking needs to mature in order to be considered as complete as ASIHTTPRequest was.

Also, ASIHTTPRequests supports client authentication, which AFNetworking lacks at the moment. The only way to implement it is to subclass AFHTTPRequestOperation and to override NSURLConnection's authentication methods. However, if you start getting involved with NSURLConnection, you will notice that putting NSURLConnection inside an NSOperation wrapper and writing completion blocks isn't so hard as it sounds and you will start thinking what keeps you from dumping 3rd party libraries.

ASI uses a whole different approach, since it uses CFNetworking (lower-level foundation frameworks based on C) to make downloading and file uploading possible, skipping NSURLConnection completely, and touching concepts most of us OS X and iOS developers are too afraid to. Because of this, you get better file uploading and downloading, even web page caches.

Which do i prefer? It's hard to say. If AFNetworking matures enough, I will like it more than ASI. Until then, I can't help but admire ASI, and the way it became one of the most used frameworks of all time for OS X and iOS.

EDIT: I think it's time to update this answer, as things have changed a bit after this post.

This post was written some time ago, and AFNetworking has matured enough. 1-2 months ago AF posted a small update for POST operations that was my last complaint about the framework (a small line ending fault was the reason that echonest uploads failed with AF but were completed fine with ASI). Authentication is not an issue with AFnetworking, as for complex authentication methods you can subclass the operation and make your own calls and AFHTTPClient makes basic authentication a piece of cake. By subclassing AFHTTPClient you can make an entire service consumer in little time.

Not to mention the absolutely necessary UIImage additions that AFNetworking offers. With blocks and custom completion blocks and some clever algorithm, you can make table views with asynchronous image downloading and cell filling pretty easily, whereas in ASI you had to make operation queues for bandwidth throttling and mind yourself to cancel and resume the operation queue according to table view visibility, and stuff like that. Development time of such operations has been halved.

I also love the success and failure blocks. ASI has only a completion block (which is actually the completion block of NSOperation). You had to check whether you had an error on completion and act accordingly. For complex web services, you could get lost in all the "ifs" and "elses"; In AFNetworking, things are much more simple and intuitive.

ASI was great for its time, but with AF you can change the way you handle web services completely in a good way, and make scalable applications more easily. I really believe that there is no reason whatsoever to stick with ASI anymore, unless you want to target iOS 3 and below.

Until now I couldn't figure out how to set a timeout with AFNetworking when doing a synchronous POST request. UPDATE: I finally figured out: https://stackoverflow.com/a/8774125/601466
Now switching to AFNetworking : ]

==================

Apple overrides the timeout for a POST, setting it to 240 seconds (in case it was set shorter then the 240 seconds), and you can't change it. With ASIHTTP you just set a timeout and it works.

A code example with a synchronous POST request:

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"doSomething", @"task",
@"foo", @"bar",
nil];


AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];


NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:requestURL parameters:params];
[httpClient release];


AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NDLog(@"fail! %@", [error localizedDescription]);
}];


NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];


[queue addOperation:operation];
[queue waitUntilAllOperationsAreFinished]; // Stuck here for at least 240 seconds!


[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
if (![[operation responseString] isEqualToString:@""]) {
return [operation responseString];
}


return nil;

I tried to set a timeout here, but nothing worked. This issue keeps me from migrating to AFNetworking.

See also here: How to set a timeout with AFNetworking

AFNetwork lacks the ability to upload large files. It assumes file content is in RAM. ASI was smart enough to simply stream file content from disk.

In ASIHTTP I loved that I could attach a userinfo dictionary to individual requests. As far as I see there is no direct support for this in AFHTTPRequestOperation. Has anyone come up with an elegant workaround yet? Apart from the trivial subclassing of course.