Skip to content

Instantly share code, notes, and snippets.

View blakewatters's full-sized avatar

Blake Watters blakewatters

View GitHub Profile
// This is typically configured as a secondary target on your project
// Dump your seed data out of your backend system in JSON format
// Add to the project as resources
// Run the secondary target in the Simulator
- (void)seedTheDatabase {
// Setup the object manager
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://restkit.org"];
objectManager.objectStore = [[RKManagedObjectStore alloc] initWithStoreFilename:@"ContactsSeed.sqlite"];
// Load all the data from the file contacts.json into a seed database
// This class is a thin model around
@interface RKPath : RKObject {
@private
NSNumber* _label;
NSArray* _route; // An array of hashes
}
@property (nonatomic, retain) NSNumber* label;
@property (nonatomic, retain) NSArray* route;
@end
@blakewatters
blakewatters / gist:1114150
Created July 29, 2011 16:18
Attach an Image to an Object Loader
[[RKObjectManager sharedManager] postObject:contact delegate:self block:^(RKObjectLoader* loader) {
RKObjectMapping* serializationMapping = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[Contact class]];
NSError* error = nil;
NSDictionary* dictionary = [[RKObjectSerializer serializerWithObject:contact mapping:serializationMapping] serializedObject:&error];
RKParams* params = [RKParams paramsWithDictionary:dictionary];
[params setData:[contact avatarImageData] MIMEType:@"image/png" forParam:@"avatar_image"];
loader.params = params;
}];
Models:
CVGroup
one-to-one relationship to CVUser as 'creator'
one-to-many relationship with users
CVUser
one-to-many relationship with groups (user belongs to many groups)
groups: // mappable keyPath. setMapping:forKeyPath:
creator. Relationship to a CVUser
@blakewatters
blakewatters / gist:1119459
Created August 2, 2011 02:16
Workaround for Managed Object Relationship Sequencing Problems
// Add `NSMutableDictionary* _managedObjectMappingsAndObjects;` to ivars and dealloc
- (void)objectMapper:(RKObjectMapper*)objectMapper didMapFromObject:(id)sourceObject toObject:(id)destinationObject atKeyPath:(NSString*)keyPath usingMapping:(RKObjectMapping*)objectMapping {
if ([objectMapping isKindOfClass:[RKManagedObjectMapping class]]) {
if (nil == _managedObjectMappingsAndObjects) {
_managedObjectMappingsAndObjects = [NSMutableDictionary new];
}
if (nil == [_managedObjectMappingsAndObjects objectForKey:objectMapping]) {
[_managedObjectMappingsAndObjects setObject:[NSMutableArray arrayWithObject:destinationObject] forKey:objectMapping];
} else {
[[_managedObjectMappingsAndObjects objectForKey:objectMapping] addObject:destinationObject];
- (void)viewDidLoad {
[super viewDidLoad];
// Configure RestKit Logging
RKLogConfigureByName("RestKit/UI", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKLogConfigureByName("RestKit/Network*", RKLogLevelDebug);
// Configure the object manager
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:4567/"];
@blakewatters
blakewatters / gist:1179996
Created August 30, 2011 02:23
RestKit Unit Testing Fixture Helpers
// Read a fixture from the app bundle
NSString* RKSpecReadFixture(NSString* fileName) {
NSError* error = nil;
NSString* filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
NSString* fixtureData = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (fixtureData == nil && error) {
[NSException raise:nil format:@"Failed to read contents of fixture '%@'. Did you add it to the app bundle? Error: %@", fileName, [error localizedDescription]];
}
return fixtureData;
}
@blakewatters
blakewatters / gist:1303723
Created October 21, 2011 12:37
Performing RestKit Mapping Operation Manually
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON];
NSError *error = nil;
id parsedObject = [parser objectFromString:JSON error:&error];
if (parsedObject) {
NSManagedObject *destinationObject = // init or fetch your target object
RKObjectMappingOperation *operation = [RKObjectMappingOperation mappingOperationFromObject:parsedObject toObject:destinationObject withMapping:yourObjectMapping];
BOOL success = [operation performMapping:&error];
}
@blakewatters
blakewatters / gist:1368793
Created November 15, 2011 23:51
sendLoaderToResourcePath:usingBlock helper for sending object loader from RKObjectManager
- (RKObjectLoader *)sendLoaderToResourcePath:(NSString *)resourcePath usingBlock:(void (^)(RKObjectLoader *objectLoader))block {
RKObjectLoader* loader = [self objectLoaderWithResourcePath:resourcePath delegate:nil];
loader.serializationMIMEType = self.serializationMIMEType;
// Yield to the block for setup
block(loader);
[loader send];
return loader;
}
@blakewatters
blakewatters / gist:1368819
Created November 16, 2011 00:00
sendData:toResourcePath:usingBlock
// Encode some arbitrary data and send it to the remote server. Object mapping will be performed on the results
- (RKObjectLoader *)sendData:(id)data toResourcePath:(NSString *)resourcePath usingBlock:(void (^)(RKObjectLoader *objectLoader))block {
RKObjectLoader* loader = [self objectLoaderWithResourcePath:resourcePath delegate:nil];
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:self.serializationMIMEType];
NSString* string = [parser stringFromObject:serializedObject error:error];
NSData* data = [string dataUsingEncoding:NSUTF8StringEncoding];
loader.params = [RKRequestSerialization serializationWithData:data MIMEType:self.serializationMIMEType];
loader.method = RKRequestMethodPOST;