This talk explains what RestKit is and how it can help you build applications that sync with REST services. The code for this talk is available at https://github.com/peterfriese/RestKitFromzeroToHero
9. Custom Headers
// send this field with each request
[client
setValue:
[[[UIDevice currentDevice] identifierForVendor] UUIDString]
forHTTPHeaderField:@"X-UDID"];
14. Background Processing
[request setBackgroundPolicy:
RKRequestBackgroundPolicyContinue];
// do noth
ing
RKRequest
Backgroun
dP olicyNone
// cancel
request
RKRequest
Backgroun
dPolicyC ancel
// continu
e until ext
RKRequest ra time exp
Backgroun ires
dPolicyCon
tinue
// requeu
e upon app
RKRequest restart
Backgroun
dPolicyReq
ueue
15. Authentication
[client
setAuthenticationType:RKRequestAuthenticationTypeHTTPBasic];
n
au thenticatio
// disable ica tionTypeNo
ne
RKReque stAuthent
on
aut o negotiati
C onnection
// NSURL io nTypeHTT
P
tA uthenticat
RKReques
h
Basic Aut sic
// HTTP Ty peHTTPBa
ut hentication
R KRequestA
// OAuth 1.0 nTypeOAu
th
atio
t Authentic
RKReques
.0
/ / OAuth 2 nT ypeOAuth
2
u thenticatio
R KRequestA
16. Request caching
client.cachePolicy =
RKRequestCachePolicyLoadIfOffline |
RKRequestCachePolicyTimeout;
// don’t u
se request
RKRequest cache
CachePolic
yNone
// use ca
che if off
RKRequest line
CachePolic
yLoadIfOf
// in case fline
of an error
RKRequest
CachePolic
yLoadOnEr
// use ET ror
ags
RKRequest
CachePolic
yEtag
// if we’ve
got data st
RKRequest ored
CachePolic
yEnabled
// in case
of a timeo
RKRequest ut
CachePolic
yTimeout
43. Core Data Integration
Change Mapped Objects
Add a Core Data Model
Register a Managed Object Store
Adjust Object Mappings
Adjust Object Creation
Fetch Data from DB / Backend
46. Add a Core Data Model
@interface GithubUser
: NSObject
47. Add a Core Data Model
@interface GithubUser
: NSObject
Keep in mind:
Assign respect
ive
classes to ma
naged
objects!
48. Register a Managed Object Store
// set up object manager
RKObjectManager *objectManager =
[RKObjectManager
objectManagerWithBaseURL:@"https://api.github.com"];
// set up backing data store
objectManager.objectStore =
[RKManagedObjectStore
objectStoreWithStoreFilename:@"github.sqlite"];
50. Adjust Object Creation
GithubIssue *issue = [[GithubIssue alloc] init];
GithubIssue *issue = [GithubIssue object];
+ (id)object {
lloc]
i d object = [[self a ty]
initWi t h E n t i t y :[ s e l f e n t i
ext:
insertIn toManagedObjectCont ead]];
ext c ontextForCurrentThr
[ NSManagedObjectCont
];
return [object autorelease
}
51. Fetch Data from DB / Backend
Online of offline?
if ([[RKObjectManager sharedManager] isOnline]) {
[self fetchDataFromRemote];
}
else {
[self fetchDataFromDataStore];
}
Offline - Fetch from DB
- (void)fetchDataFromDataStore {
NSFetchRequest *request =
[[[RKObjectManager sharedManager] mappingProvider]
fetchRequestForResourcePath:self.resourcePath];
self.repos = [GithubRepo objectsWithFetchRequest:request];
[self.tableView reloadData];
}
52. Fetch Data from DB / Backend
Online - Fetch from Backend
- (void)fetchDataFromRemote {
[[RKObjectManager sharedManager]
loadObjectsAtResourcePath:[self resourcePath]
usingBlock:^(RKObjectLoader *loader) {
[loader setOnDidLoadObjects:^(NSArray *objects) {
self.repos = objects;
[self.tableView reloadData];
}];
}];
}
53. Fetch Data from DB / Backend
Reconnect after offline
- (void)reachabilityChanged:(NSNotification*)notification {
RKReachabilityObserver* observer =
(RKReachabilityObserver*)[notification object];
if ([observer isNetworkReachable]) {
if (![self.view isHidden]) {
[self fetchDataFromRemote];
}
}
else {
if (![self.view isHidden]) {
[self fetchDataFromDataStore];
}
}
}
65. Anyway, Thanks!
Peter Friese
om
peter.friese@zuehlke.c
hlke Engineering Gm
bH
Available for cons
Zü
Am Sandtorkai 66 ulting,
20457 Hamburg projects, discuss
ing
+49 151 108 604 72 all things mobile
- and frosty bever
ages!
CouchDB Mobile - from Couch to 5K in 1 Hour