RestKit is an iOS framework that simplifies communication with RESTful web services. It includes a networking layer for sending and receiving HTTP requests, object mapping for serializing responses to Objective-C objects, integration with Core Data for storage, and pluggable parsing layers for different data formats like JSON and XML. RestKit handles common tasks like authentication, request queuing, response parsing, and object lifecycles to provide a complete solution for working with REST APIs in iOS applications.
16. "created_at":"Sat Mar 05 13:39:09 +0000 2011",
"favorited":false,
"id":44029179364253700,
"in_reply_to_screen_name":"JustJenFelice",
"text":"@JustJenFelice Be sure to join the Google Group and
reach out if you need any support!",
"user":{
"id":208727870,
"name":"RestKit",
"screen_name":"RestKit",
}
16
24. Configuring CoreData
#import <RestKit/CoreData/CoreData.h>
- (void)configureObjectStore {
// Initialize the RestKit Object Manager
RKObjectManager* objectManager = [RKObjectManager
objectManagerWithBaseURL:@"http://restkit.org"];
!
// Initialize object store
// We are using the Core Data support, so we have initialized a managed
object store backed
// with a SQLite database.
objectManager.objectStore = [RKManagedObjectStore
objectStoreWithStoreFilename:@"Contacts.sqlite"];
}
24
26. Working with Core Data
// Adapted from https://github.com/magicalpanda/MagicalRecord
// Get all the Contacts
NSArray* contacts = [Contact allObjects];
!
// Count the Contacts
NSError* error = nil;
NSUInteger count = [Contact count:&error];
!
// Find Contact by primary key
NSNumber* contactID = [NSNumber numberWithInt:12345];
Contact* somebody = [Contact objectWithPrimaryKeyValue:contactID];
!
// Find Contacts with criteria
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"name
contains[cd] 'restkit'"];
NSArray* matches = [Contact objectsWithPredicate:predicate];
26
27. Database Seeding
NSString *seedDatabaseName = nil;
NSString *databaseName = RKDefaultSeedDatabaseFileName;
objectManager.objectStore = [RKManagedObjectStore
objectStoreWithStoreFilename:databaseName
usingSeedDatabaseName:seedDatabaseName managedObjectModel:nil
delegate:self];
RKManagedObjectSeeder* seeder = [RKManagedObjectSeeder
objectSeederWithObjectManager:objectManager];
// Seed DB with instances of RKTStatus from a snapshot of the RestKit
Twitter timeline
[seeder seedObjectsFromFile:@"restkit.json"
withObjectMapping:statusMapping];
// Seed DB with RKTUser objects. The class will be inferred via element
registration
[seeder seedObjectsFromFiles:@"users.json", nil];
// Finalize the seeding and output a helpful informational message
[seeder finalizeSeedingAndExit];
27