afnetworking vs. native + caching

11

Click here to load reader

Upload: klabcyscorpions-techblog

Post on 06-May-2015

3.442 views

Category:

Education


4 download

TRANSCRIPT

Page 1: AfNetworking vs. Native + Caching

AFNetworking  vs.  Na2ve  NSURLConnec2on  &    

NSURLSession  +  Caching  

Page 2: AfNetworking vs. Native + Caching

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.  

Page 3: AfNetworking vs. Native + Caching

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.  

Page 4: AfNetworking vs. Native + Caching

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.  

Page 5: AfNetworking vs. Native + Caching

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.  

Page 6: AfNetworking vs. Native + Caching

 

 -­‐  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).  

Page 7: AfNetworking vs. Native + Caching

Let’s  compare!  NSURLConnec-on   NSURLSession

AFNetworking

A simple request to API/Server using completion blocks.

Page 8: AfNetworking vs. Native + Caching

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.  

Page 9: AfNetworking vs. Native + Caching

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.  

Page 10: AfNetworking vs. Native + Caching

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.  

Page 11: AfNetworking vs. Native + Caching

Conclusion  

NSURLConnect

NSURLSession

AFNetworking

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. All of them are capable of using Cache policy in communication of server Cache-Control header. AFNetworking has a great category method in UIImageView that automatically request the image and cache it while the app is running.