0:00:00Hello and welcome to developing perspective developing perspective is a podcast discussing news of note in iOS Apple and the like
0:00:07I'm your host David Smith. I'm an independent iOS developer based in Herndon, Virginia. This is show number 30. And today I'm going to be talking about
0:00:14iOS networking
0:00:18Specifically kind of how I do it my preferences things that I've learned over kind of doing this for a couple of years
0:00:22And I'll kind of walk through what I prefer to do some of the place I've been burned in the past and so on
0:00:28All right
0:00:30So when I say networking what I mean is essentially how I get data from
0:00:35Yes, so some networking URL and put that into my app whether I'm downloading a file like I do in my audiobooks application
0:00:44Which downloads lots of mp3s?
0:00:46Whether I'm just reading a URL to get some data from a web service
0:00:50Say like for example in the app called civil gram that downloads
0:00:54Data from the Instagram API, so making lots of JSON requests and things to kind of manage that
0:00:59And really however I do it. I'm always sort of opening URL and getting some data and then doing something with it
0:01:05it's a very common paradigm and it's probably one of the things that almost every
0:01:10iOS app I've ever worked on had some form of
0:01:13And so basically that you know you'd think oh, it's free
0:01:18You know it's surface all solve problem at that point, but the tricky thing with networking is hey
0:01:23hey, there's a lot of things that can go wrong.
0:01:26There's a lot of places where,
0:01:27especially on a mobile device like an iPhone,
0:01:30where all of a sudden they're on 3G
0:01:31and the network just disappears for a minute or something,
0:01:34or they're in a car driving
0:01:36and they're switching cell towers
0:01:37and all kinds of weird issues can kind of come up.
0:01:41And then sort of compounding that
0:01:44is Apple provides not that great,
0:01:46it's sort of, or certainly not high level APIs
0:01:52dealing with networking. Almost all the Apple-provided stuff revolves around NSURLConnection, which
0:01:59is a fine class that lets you open a URL request, download the contents as you go, reject the
0:02:05status and so on. But that typically exposes too many details to the developer when they
0:02:12really just want, "Open this URL and give me the results or tell me it failed. I don't
0:02:18I don't need to know all the details,
0:02:20I don't need to know all the memory management
0:02:22of where is this, how much data have I got,
0:02:24what's the progress, and so on.
0:02:26And so typically what developers have done traditionally
0:02:29is they've built wrappers around that.
0:02:31For a very long time, the big winner in terms of popularity
0:02:37was a thing called ASI HTTP request,
0:02:40which is currently kind of, I wouldn't say defunct
0:02:43is the wrong word, but is no longer being actively developed.
0:02:46about the guy who wrote it and kind of, you know, I don't want to get into that for a
0:02:52while, that was sort of the big thing. And then a variety of others have kind of sprung
0:02:55up over time. And I think they solve the problem better typically than the way that Apple's
0:03:04API provides. I would say one thing that's interesting is that on the Mac in 10.7, Apple
0:03:11some nice block-based updates to nsu-oral connection that hopefully will one day find their way to the iOS
0:03:17Which solve a lot of these problems and what I would love to be able to drop
0:03:22You know a third-party dependency like this and just use the Apple stuff. It's not quite ready yet
0:03:28So anyway, so I'm gonna kind of talk through the way I do this and kind of what I do
0:03:33Right now my kind of go-to networking stack is AF networking
0:03:38Which there's a link in the show notes if you're interested in and looking it up
0:03:43Which is a networking API that was developed by the team at Gowalla
0:03:47I think it's what they are they developed and internally for their projects and then open-sourced it. It's a MIT licensed
0:03:56You know products are basically which basically means you can use it however you want and
0:04:03you
0:04:05without restrictions. It's not a VSD license or something that you have to do attribution with,
0:04:12though it's always nice to do attribution. You can basically just do what you want with this.
0:04:16And so, what I like about AF networking is two things.
0:04:21One is it provides a really nice block-based API for network operations,
0:04:26and that's really all I want. Since iOS 4 came along and allowed you to do blocks,
0:04:32it makes the code you write for networking so much easier,
0:04:36rather than being delegate-based.
0:04:38So rather than having to build these kind of complicated
0:04:40sort of callback delegate structures that you have to keep track of your state and things,
0:04:46you can do these really nice things where you say,
0:04:48in this block of code, you build a request, make the request,
0:04:52and then it's like, if it fails, call this block.
0:04:55If it succeeds, call this block.
0:04:58If you have a progress update, call this block.
0:05:01And that just kind of works.
0:05:03It keeps your code really straightforward.
0:05:05It keeps it all in one place,
0:05:07which I think makes maintainability hugely easier.
0:05:11Because rather than having this problem of,
0:05:13"Okay, where does this, what is this delegate callback
0:05:15getting called from, and what's going on here, here, and here?"
0:05:19It's all just in one place.
0:05:21You look at it and it's like, "Okay, they made a request
0:05:23to get user feed."
0:05:25And then here, if it fails,
0:05:27they display an error message, or they do whatever they need.
0:05:29need. Here's a progress update. So if you're doing some like downloading a file, you can say okay,
0:05:33you know, I've done 10%, 20%, 30%, and then if it succeeds, okay, now here it goes, and if it goes
0:05:40and parses it or does whatever. And I found that to be really helpful. Typically, I mean, using a
0:05:45third-party library is always a little bit risky. You never really know what you're getting yourself
0:05:50into. And the reason I feel pretty good about AF networking right now is that it's just, it's
0:05:55promoted and put out by a organization rather than a individual.
0:06:01So this is, I imagine, used, was developed by Goaala internally, and then they decided to open source it.
0:06:07Goaala is not like it's a huge company, it's not like it was built by Apple or IBM or something,
0:06:12but they're a sizable startup that's doing fairly well.
0:06:18And it seems to have a fairly high activity level on GitHub, which is always a good thing.
0:06:22There's 1,700 watchers and 211 fork requests, or forks of it.
0:06:30So people are kind of playing with it and using it.
0:06:33As a comparison, one that I did look at
0:06:36is MKNetworkKit, which is written by Munguth Kumar, which
0:06:40is another pretty good block-based networking
0:06:43stack that I kind of looked at and played with.
0:06:45But in the end, the thing that I ended up
0:06:47going with AF networking is just that stability
0:06:50and the, I guess maybe the dependability
0:06:55that AF networking provides,
0:06:57that I'm fairly confident that it'll be updated
0:06:59as it goes forward, that it'll be worked on and so on
0:07:03in a way that, with something that's a smaller project
0:07:06by an individual, it's not to say that it's bad,
0:07:08but it just doesn't give me the same warm fuzzies
0:07:11that a larger project with lots of followers,
0:07:14lots of activity on it kinda does.
0:07:17So that's basically what I use in my apps now.
0:07:18I just throw in AF networking, it's about five or six classes
0:07:23that you add to your app.
0:07:26One thing I will say is that it has a bunch of
0:07:28kind of advanced features, I guess you could say,
0:07:30where it's like, oh, here's a JSON request,
0:07:32and give me the, rather than returning a data object,
0:07:34it returns a dictionary or an array of objects,
0:07:37or you can do an XML thing.
0:07:41Those I never really use, and you can also do this thing
0:07:43where you give it a UI image view,
0:07:45and it updates the UI image view kind of behind the scenes
0:07:48and the data's available and so on.
0:07:49I tend to stay away from those,
0:07:50and generally I'd recommend kind of avoiding
0:07:53any kind of magic in the application.
0:07:56But I want my networking stack to do is say,
0:07:59here's a URL, give me the data, and that's it.
0:08:02And then from there, I'm gonna take it
0:08:04and I'm gonna work with that data.
0:08:05Because of the fewer things that I'm relying
0:08:08on this third party to do, I think typically
0:08:11the more reliable and maintainable my code's gonna be.
0:08:14So as soon as it, you know, it's like,
0:08:15If there's some weird JSON parsing bug,
0:08:18it's going to be harder for me to realize
0:08:19that's happening inside of AF networking
0:08:23than if it was just part of my own code,
0:08:25and I can see, okay, here's the data I got back.
0:08:28That looks correct.
0:08:29So then it's my problem,
0:08:31rather than this kind of two-step problem
0:08:33of am I getting the right data back,
0:08:34and then am I parsing it correctly,
0:08:36and then am I doing something wrong with it?
0:08:38So I would definitely kind of stay away from most of those.
0:08:41It's kind of cool in some ways,
0:08:43like you can kind of simplify your code,
0:08:44but I think overall, I would rather do my own kind of wrappings around those things.
0:08:48But that's just kind of the way I roll, I guess.
0:08:51Let's see, other thoughts about networking stacks.
0:08:54Things that you should keep in mind is that they're online especially.
0:08:59There's a great tool called the Network Link Conditioner,
0:09:02which is a little thing that's installed with the SDK
0:09:06sender, I think applications, utilities, that lets you configure your regular Macs, you
0:09:15know, Wi-Fi or wired Ethernet connection to act as though it's a dubious connection, act
0:09:20as though it's a 3G network, act as though it's an edge network, randomly drop packets,
0:09:25do all kinds of things like that. That is really helpful for testing and debugging,
0:09:30because typically if you're in the simulator you can do a much quicker round trips, your
0:09:34performance of the app so much better, which is not great for performance testing, but
0:09:38for as you're testing and iterating on something, it's much quicker of a feedback loop that
0:09:43you can get.
0:09:44You can use the networking conditioner to play with that.
0:09:47Of course, that's no substitute for testing on a device, but I found that to be kind of
0:09:52a helpful thing to just be like, "Hey, let's see what happens."
0:09:55And also it lets you test in circumstances that are often difficult to recreate on a
0:09:59device.
0:10:00it's like if I want to test a really bad edge,
0:10:04you know, 2G connection on my iPhone,
0:10:06I'd need to turn off 3G and then drive
0:10:10to the middle of nowhere, which can be kind of awkward
0:10:13and kind of annoying to do, you know,
0:10:16'cause my office gets great cell phone reception,
0:10:18I'm really near a cell tower.
0:10:20So it's kind of nice to be able to test
0:10:21and do those kinds of extreme tests
0:10:23without having to physically test it and do it.
0:10:27I remember back when I was first working on audiobooks
0:10:29back in the day, I remember doing testing
0:10:32where I would take my iPhone and wrap it and unwrap it
0:10:36in aluminum foil, which, if you've never done it,
0:10:40if you wrap a phone in aluminum foil,
0:10:42the cell signal disappears 'cause you're building it
0:10:45inside of a Faraday cage.
0:10:46And so I would sit there and wrap and unwrap
0:10:49and wrap and unwrap to simulate packet loss
0:10:51and to deal with those kinds of things.
0:10:53I can definitely say using the Network Link Conditioner
0:10:55is a much better choice than doing something like that.
0:10:59One of the other things about networking is to just generally be careful with it.
0:11:05I mean, I think I take a very pragmatic use on it, where some people go kind of crazy
0:11:09about "oh, what happens if this..." you know, it's like dealing with failure and so on.
0:11:13And I think in my experience, users are fairly tolerant of, if they're on a bad connection,
0:11:18they're going to know that and be somewhat tolerant of that failure.
0:11:23And your goal in that case is to never put the app into an unresponsive or inconsistent
0:11:28But I don't tend to do too much of like,
0:11:31"Okay, I'm going to keep trying to retry it,
0:11:33and retry it, and retry it."
0:11:34Or, you know, just sort of trying to do two smart things.
0:11:37If it fails, I'm just going to tell the user,
0:11:39"Hey, it looks like your network connection's a little spotty right now.
0:11:42Can you check on that and try again?"
0:11:45Which may or may or may not be good.
0:11:47I think that tends to work better, I find,
0:11:50than to kind of be trying and trying and trying,
0:11:54and the user doesn't really know what's happening.
0:11:56It looks like it looks like it's downloading, it looks like it's not.
0:12:00So just sort of some thoughts.
0:12:02Alright, that's it for today's show.
0:12:04As always, if you have any thoughts, questions, comments, feedback, hit me up on Twitter.
0:12:08I am @_davidsmith.
0:12:09I blog at david-smith.org.
0:12:13And otherwise, I hope you have a good day.
0:12:15Happy coding, and I'll talk to you later.
0:12:16[BLANK_AUDIO]
0:12:34[ Silence ]