This document compares AFNetworking and NSURLSession for making network requests in iOS, and describes how to implement caching. AFNetworking makes common networking tasks easier by providing convenient methods, while NSURLSession is Apple's preferred networking API introduced in iOS 7. It also supports features like background transfers and pause/resume of requests. Caching can be implemented using NSURLCache, which stores responses locally to improve performance and offline usage.
2. What
is
AFNetworking?
AFNetworking
is
a
class
developed
by
MaD
Thompson
and
ScoD
Raymond
and
is
described
as
“a
delighHul
networking
library
for
iOS
and
Mac
OS
X.”
It
makes
common
tasks
like
asynchronous
hDp
requests
a
lot
easier.
AFNetworking
just
like
all
other
popular
libraries
to
do
the
same
all
use
NSURLConnec2on
to
do
the
actual
'work'.
They
just
include
a
bunch
of
friendly
methods
so
you
can
easily
make
requests
without
having
to
write
a
whole
lot
of
boilerplate
code
yourself.
3. Several
AFNetworking
benefits
-‐
One
of
the
benefits
of
using
AFNetworking
is
the
data
type
classes
for
handling
response
data.
The
success
block
has
already
parsed
the
response
and
returns
the
data
for
you.
With
NSURLSession
you
receive
NSData
back
in
the
comple2on
handler,
so
you
would
need
to
convert
the
NSData
into
JSON
or
other
formats.
-‐
It
also
helps
you
ensure
that
your
UI
is
responsive
even
when
your
app
is
in
the
middle
of
a
big
download.
-‐
AFNetworking
adds
a
category
to
UIImageView
that
lets
you
load
images
asynchronously,
meaning
the
UI
will
remain
responsive
while
images
are
downloaded
in
the
background.
4. What
is
NSURLSession?
In
iOS
7,
Apple
has
introduced
NSURLSession,
which
is
a
suite
of
classes
that
replaces
NSURLConnec-on
as
the
preferred
method
of
networking.
Using
NSURLSession
is
just
as
easy
as
using
its
predecessor
NSURLConnec-on
for
simple
tasks.
5. Several
NSURLSession
new
advantages
and
benefits
-‐
NSURLSession
is
created,
you
get
all
the
benefits
of
background
networking.
This
helps
with
baDery
life,
supports
UIKit
mul2tasking
and
uses
the
same
delegate
model
as
in-‐
process
transfers.
-‐
Ability
to
pause
and
resume
networking
opera-ons:
With
the
NSURLSession
API
any
networking
task
can
be
paused,
stopped,
and
restarted.
No
NSOpera2on
sub-‐classing
is
necessary.
-‐
Instead
of
storing
all
of
the
networking
objects
(such
as
a
response
cache)
globally,
NSURLSession
provides
a
mechanism
for
storing
objects
either
on
a
global
basis
or
on
a
per
session
basis.
6.
-‐
Rich
delegate
model:
NSURLConnec-on
has
some
asynchronous
block
based
methods,
however
a
delegate
cannot
be
used
with
them.
When
the
request
is
made
it
either
works
or
fails,
even
if
authen2ca2on
was
needed.
With
NSURLSession
you
can
have
a
hybrid
approach,
use
the
asynchronous
block
based
methods
and
also
setup
a
delegate
to
handle
authen2ca2on.
-‐
Uploads
and
downloads
through
the
file
system:
This
encourages
the
separa2on
of
the
data
(file
contents)
from
the
metadata
(the
URL
and
se]ngs).
8. Web
Caching
A
web
cache
is
a
mechanism
for
the
temporary
storage
(caching)
of
web
documents,
such
as
HTML
pages
and
images,
to
reduce
bandwidth
usage,
server
load,
and
perceived
lag.
A
web
cache
stores
copies
of
documents
passing
through
it.
A
client,
such
as
a
web
browser,
can
store
web
content
for
reuse.
For
example
:
if
the
back
buDon
is
pressed,
the
local
cached
version
of
a
page
may
be
displayed
instead
of
a
new
request
being
sent
to
the
web
server.
9. How
do
we
cache?
NSURLCache
provides
a
composite
in-‐memory
and
on-‐disk
caching
mechanism
for
URL
requests
to
your
applica2on.
As
part
of
Founda2on's
URL
Loading
System,
any
request
loaded
through
NSURLConnec2on
will
be
handled
by
NSURLCache.
Network
caching
reduces
the
number
of
requests
that
need
to
be
made
to
the
server,
and
improve
the
experience
of
using
an
applica2on
offline
or
under
slow
network
condi2ons.
When
a
request
has
finished
loading
its
response
from
the
server,
a
cached
response
will
be
saved
locally.
The
next
2me
the
same
request
is
made,
the
locally-‐cached
response
will
be
returned
immediately,
without
connec2ng
to
the
server.
NSURLCache
returns
the
cached
response
automa&cally
and
transparently.
10. NSURLRequestCachePolicy
Caching
policies
are
specified
in
both
the
request
(by
the
client)
and
in
the
response
(by
the
server).
NSURLRequest
has
a
cachePolicy
property,
which
specifies
the
caching
behavior
of
the
request
according
to
the
following
constants:
NSURLRequestUseProtocolCachePolicy
=
default
behavior.
NSURLRequestReloadIgnoringLocalCacheData
=
don’t
use
the
cache.
NSURLRequestReloadIgnoringLocalAndRemoteCacheData
=
seriously
don’t
use
the
cache.
NSURLRequestReturnCacheDataElseLoad
=
Use
the
cache
no
ma9er
how
out
of
date,
or
if
no
cached
response
exists,
load
from
the
network.
NSURLRequestReturnCacheDataDontLoad
=
Offline
mode.
Always
use
the
cache
no
ma9er
how
out
of
date.
NSURLRequestReloadRevalida-ngCacheData
=
Validate
cache
against
server
before
using.
11. Conclusion
NSURLSession
NSURLSession can do every
NSURLConnection can do and
AFNetworking can do every
NSURLConnection and NSURLSession
can do. But you can also do
AFNetworking can do using
NSURLConnection and NSURLSession
just a little bunch of codes.
AFNetworking
All of them are capable of using Cache
policy in communication of server CacheControl header.
NSURLConnect
AFNetworking has a great category
method in UIImageView that automatically
request the image and cache it while the
app is running.