Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lifebit iOS - Kusanagi and some tips

Jayson Basañes
June 06, 2014
52

Lifebit iOS - Kusanagi and some tips

Jayson Basañes

June 06, 2014
Tweet

Transcript

  1. AFNetworking  2.0   •  AFHTTPRequestOpera9onManager   – requestSerializer   – responseSerializer  

    •  AFNetworkReachabilityManager   •  AFNetworkAc9vityIndicatorManager  
  2. Kusanagi  -­‐  KSAPI   •  BDBOAuth1RequestOpera9onManager   – requestSerializer   • 

    KSRequestOpera9onManager   –  HTTPRequestOpera9onWithRequest:success:failure   –  POSTJSON   •  AFNetworking  handles  401s  differently   –     
  3. Mantle   •  Parsing  of  JSON  responses  and  conver9ng  

    them  to  MTLModel   •  Saving  of  MTLModel  to  CoreData   •  MTLModel  <-­‐-­‐-­‐-­‐>  NSManagedObject   •  We  work  with  MTLModel,  not  with   NSManagedObject  
  4. Y  U  NO  USE  NSManagedObject?   •  Because  CoreData  objects

     are  generally  not   thread  safe.     •  Different  contexts  (NSManagedObjectContext)   for  different  threads   •  It’s  safer  to  pass  around  pure  NSObjects   (MTLModel)  along  different  threads  
  5. Mantle    -­‐-­‐>  Core  Data   •  Uniquing  (if  there’s

     such  a  word)   •  Some  `meta`  fields  can  just  be  Transformable  
  6. MagicalRecord   •  Cleans  up  all  the  shit  we  do

     with  Core  Data   •  Manage  contexts   –  NSManagedObjectContext+MR_rootSavingContext   – MagicalRecord+saveWithBlock:comple9on   •  Easy  queries   –  NSManagedObject-­‐MR_findFirstByAeribute:withValue:inContext  
  7. MagicalRecord   •  When  doing  queries,  use  the  context  from

      [NSManagedObjectContext+MR_contextForCurrentThread]   •  Remember  that  queries  return   NSManagedObject  instances,  not  MTLModel   •  Use:   – MTLModel+modelsFromManagedObjects   – MTLModel+modelFromManagedObject  
  8. Kusanagi  -­‐  KSDataStore   •  Has  no  other  purpose  but

     to  ini9alize   MagicalRecord   •  We  might  move  some  Core  Data  stuffs  here   later  to  abstract  MagicalRecord.  
  9. Kusanagi   •  Separate  project.  Added  to  Lifebit  as  local

      Pod.   •  Kusanagi.xcdatamodel   – Remember  that  object  dele9on  rules  are  in   reverse   •  95%  of  the  9me,  JSON  responses  are  saved  to   Core  Data.   •  JSON  “.me”  responses  are  saved  as  “facts”  
  10. Kusanagi  -­‐  some  rules   •  KSAPI  methods  should  map

     to  the  real  API   endpoints.   •  When  adding  new  files   1.  Add  them  to  Kusanagi.xcodeproj     2.  Update  version  of  Kusanagi.podspec   3.  Run  `pod  update`  in  Lifebit  workspace   •         
  11. Kusanagi  -­‐  some  rules   •  JSON  transforma9ons  and  saving

     should  be  in   their  specific  classes   •  Use  a  single  NSManagedObjectContext  as   much  as  possible.   •  Enclose  JSON  transformers  in   +JSONDic9onaryTransformer  or   +JSONArrayTransformer  
  12. Kusanagi  -­‐  some  rules   •  Custom  methods  for  queries

     should  be  in   Lifebit  (+FFExtensions):   – KSQBTimelineItem+findForFeedOfRequestorId:   •  Other  app  specific  stuffs  should  be  in   +FFExtensions:   – KSPlace+urlStringForSta9cMapWithSize:  
  13. Simple  LLDB  commands   •  po   •  Print  

    •  dot-­‐nota9on  doesn’t  work  
  14. Advanced  breakpoints   •  You  don’t  have  to  stop  the

     program  just  to   add  logging   •  Condi9ons   •  Ac9ons   – Log  Message   – Debugger  Command   – Sound   •  No  stopping  
  15. Others   •  Reveal  in  Project  Navigator   •  Searching

     with  Regex   •  Simulator   – Toggle  Slow  Anima9ons   – Color  Blended  Layers   •  Red:  blending   •  Green:  no  blending  
  16. AppCode  FTW   •  Find  Usages   – Includes  XIB  files!

        •  Inspect  Code   •  Refactoring   – Rename     – Change  Signature  
  17. AppCode  L   •  Code  hin9ng  is  slow    

    •  No  UI  designer  –  no  biggie  
  18. MVVM   •  ViewModels  are  moderately  aware  of  what  

    the  view  will  look  like  (e.g.  LoginForm)   •  Controllers  should  just  “react”  to  it’s   ViewModel.     •  Cells  can  have  their  own  ViewModels  too.  
  19. Classes   •  Header  file   – #import  superclass  and  conformed

     protocols   – Forward  declara9on  for  everything  else     •  Implementa9on  file     – #import  everything  you  use   – Use  <Library/Library.h>  conven9on  for  impor9ng   3rd-­‐party  libraries  
  20. The  thing  with  ivars   •  Stop  directly  using  ivars

     (`_property`).  Use   `self.property`.   •  Proper9es  launch  KVO  no9fica9ons   •  Proper9es  are  atomic  (cannot  be  accessed   from  more  than  one  thread)  -­‐  safer  to  read   and  write  in  mul9ple  threads   •  Prevent  accidents  when  overriding  proper9es  
  21. Others   •  @strongify  and  @weakify  all  the  things  

    –  API  calls   –  RAC  handlers   –  Not  needed  in  UIView-­‐animateWithDura9on:     •  Make  sure  you  are  using  SDWebImage  when   loading  remote  images   •  Avoid  singletons.  Make  app  services  instead.   •  -­‐tableView:es9matedHeightForRowAtIndexPath    
  22. Remember   •  AFNetworking  callbacks  are  run  on  the  main

      thread.   •  Do  not  repeat  the  nightmare  that  was   `LBUser`