Share
Embed
Lifebit iOS - Kusanagi and some tips
Jayson Basañes
June 06, 2014
Transcript
35 slides
-
AFNetworking 2.0 • AFHTTPRequestOpera9onManager – requestSerializer – responseSerializer
• AFNetworkReachabilityManager • AFNetworkAc9vityIndicatorManager -
Kusanagi -‐ KSAPI • BDBOAuth1RequestOpera9onManager – requestSerializer •
KSRequestOpera9onManager – HTTPRequestOpera9onWithRequest:success:failure – POSTJSON • AFNetworking handles 401s differently – -
Mantle • Parsing of JSON responses and conver9ng
them to MTLModel • Saving of MTLModel to CoreData • MTLModel <-‐-‐-‐-‐> NSManagedObject • We work with MTLModel, not with NSManagedObject -
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 -
Mantle -‐-‐> Core Data • Uniquing (if there’s
such a word) • Some `meta` fields can just be Transformable -
Reverse Mantle • NSManagedObject -‐-‐> MTLModel • MTLModel
-‐-‐> NSDic9onary or NSArray -
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 -
MagicalRecord • When doing queries, use the context from
[NSManagedObjectContext+MR_contextForCurrentThread] • Remember that queries return NSManagedObject instances, not MTLModel • Use: – MTLModel+modelsFromManagedObjects – MTLModel+modelFromManagedObject -
Kusanagi -‐ KSDataStore • Has no other purpose but
to ini9alize MagicalRecord • We might move some Core Data stuffs here later to abstract MagicalRecord. -
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” -
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 • -
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 -
Kusanagi -‐ some rules • Custom methods for queries
should be in Lifebit (+FFExtensions): – KSQBTimelineItem+findForFeedOfRequestorId: • Other app specific stuffs should be in +FFExtensions: – KSPlace+urlStringForSta9cMapWithSize: -
Simple LLDB commands • po • Print
• dot-‐nota9on doesn’t work -
Advanced breakpoints • You don’t have to stop the
program just to add logging • Condi9ons • Ac9ons – Log Message – Debugger Command – Sound • No stopping -
Keyboard shortcuts • CMD+[1-‐8] • CMD+Opt+[1-‐2] •
Others: Look at the Xcode menu -
Others • Reveal in Project Navigator • Searching
with Regex • Simulator – Toggle Slow Anima9ons – Color Blended Layers • Red: blending • Green: no blending -
AppCode FTW • Find Usages – Includes XIB files!
• Inspect Code • Refactoring – Rename – Change Signature -
AppCode L • Code hin9ng is slow
• No UI designer – no biggie -
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. -
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 -
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 -
Only use ivars… • Inside `-‐init` constructors •
Property overrides -
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 -
Remember • AFNetworking callbacks are run on the main
thread. • Do not repeat the nightmare that was `LBUser` -
Things to think about • Reac9veCocoa – reac9ve programming
• Auto-‐layout • Swiy