This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[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; | |
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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/"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
OlderNewer