0

in iOS development, I have often seen people creating a networking module to interact with their APIs.

This module generally sits on top of a networking framework like MKNetWorkKit or AFNetWorking.

In most of the cases, It's all about sending GET,POST request and parsing the response which is in most cases JSON.

What extra practical benefits that these libraries provide that an iOS developer should be leveraging which the plain Cocoa Networking APIs lack ?

I can understand RESTKit as one exception where it takes care of the conversion of JSON to native objects and also interfaces with Core Data but what about others ?

2 Answers 2

3

What extra practical benefits that these libraries provide that an iOS developer should be leveraging which the plain Cocoa Networking APIs lack ?

Error handling, ease of use, simplified code, etc. NSURLConnection is a great class and very flexible, but if you take a look at NSURLConnectionDelegate you'll see that there are quite a few different network states that have to be handled. A good networking library takes care of much of that routine work.

3
  • So if someone is making a product of their own which they will be maintaining for a long time. Is it a good idea to write ur own wrapper library on top of NSURLConnection etc Or use one of these existing libraries ? Commented Jul 2, 2013 at 7:04
  • @AmoghTalpallikar Depends... is there a 3rd party library that you like? Will it save you time compared to rolling your own? Can you live with the terms of the license? It's the same decision you'd make for any other library.
    – Caleb
    Commented Jul 2, 2013 at 7:27
  • hmm. yeah make sense. But is it still important for one to understand how NSURLConnection and related classes work ? Frankly speaking, it has been one year since I have been doing iOS dev. All I have been using is, always one of these libraries. I have never used NSURLConnection directly. I came from a JavaScript/AJAX background. Where I am used to creating an XMLHTTPRequest Object and give it a callback and done. I found working with these libraries, specially block based ones quite similar. So I was wondering, if I will ever need to understand NSURLConnection at all. Commented Jul 2, 2013 at 7:42
2

One of the goals may be to abstract the way the network data is accessed.

Such abstraction can be very abstract:

public loadProducts(currentCategory) returns list of Product {
    var productsFactory = dataAccess.factory.createProductsFactory();
    var productsOfCategory = productsFactory.loadProducts(currentCategory);
    return productsOfCategory;
}

Here, the caller don't know if products are loaded from a database, an XML file, a REST request or something completely different. It makes it very easy to replace one data source by another, for example migrate all data from a REST service to a database.

Or the abstraction may be more transparent:

public doPostRequest(parameters) returns Stream {
    ... // Use library A to do the request. if you need to switch to library B later, this
        // is not an issue.
}

Here, the caller knows that the library is using a network resource. While being transparent, such library can still be useful when migrating from one way to do HTTP requests to another.

In .NET Framework, for example, there are at least three ways to do HTTP requests, not counting all the third party libraries one can use. Picking one way and sticking with it is a solution, but later, migrating to another implementation would be painful. Another solution is to create your own layer, abstracting the actual implementation: if a new version of .NET Framework will be released and you want to use a new way of doing HTTP requests, you change the code in one place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.