What major ASIHTTPRequest features is AFNetworking

2019-01-16 00:26发布

With work having recently stopped on ASIHTTPRequest, it seems like attention is shifting to AFNetworking.

However, I've not yet found a good comparison of the features of the two libraries, so I don't know what I might lose if/when I switch over.

Major differences I've spotted so far are:

  1. AFNetworking has a much smaller code size (which is good)
  2. AFNetworking is being rapidly improved (so it may not yet be mature, may not have a stable API yet?)
  3. Both seem to have caching, though I've seen hints that because AFNetworking uses NSURLConnection it won't cache objects over 50K
  4. ASIHTTPRequest has very good support for manual & automatic (PAC) http proxies; I can't find any information on what level of support AFNetworking has for proxies
  5. AFNetworking requires iOS 4+, whereas ASIHTTPRequest works right back to iOS 2 (not really an issue for me, but it is an issue for some people)
  6. AFNetworking doesn't (yet) have a built in persistent cache, but there's a persistent cache which has a pending pull request: https://github.com/gowalla/AFNetworking/pull/25

Has anyone seen any good comparisons of the two libraries or any documented experiences of switching from one to the other?

8条回答
Bombasti
2楼-- · 2019-01-16 00:44

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.

查看更多
迷人小祖宗
3楼-- · 2019-01-16 00:47

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*.

查看更多
Viruses.
4楼-- · 2019-01-16 00:52

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

查看更多
贪生不怕死
5楼-- · 2019-01-16 00:57

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.

查看更多
放我归山
6楼-- · 2019-01-16 01:00

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.

查看更多
够拽才男人
7楼-- · 2019-01-16 01:09

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.

查看更多
登录 后发表回答