Skip to content

Instantly share code, notes, and snippets.

@rconnelly
Created December 20, 2011 07:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rconnelly/1500662 to your computer and use it in GitHub Desktop.
Save rconnelly/1500662 to your computer and use it in GitHub Desktop.
Example of how to set up a stackmob application with restkit.
STMRestKitConfiguration *config = [STMRestKitConfiguration configuration];
// UserModel is a class which implements the STMMappedObject protocol
// this line registers the model
NSArray *mappings = [STMObjectMapping defaultMappingsForClasses:[UserModel class], nil];
[config.mappingProvider registerObjectMappings:mappings];
// create a restkit data provider
id<STMDataProvider> dataProvider = [STMRestkitDataProvider dataProviderWithConfiguration:config];
// set the application with the restkit data provider
[StackMob setApplication:STACKMOB_PUBLIC_KEY
secret:STACKMOB_PRIVATE_KEY
appName:STACKMOB_APP_NAME
subDomain:STACKMOB_APP_SUBDOMAIN
userObjectName:STACKMOB_USER_OBJECT_NAME
apiVersionNumber:[NSNumber numberWithInt:STACKMOB_API_VERSION]
dataProvider:dataProvider];
#import <Foundation/Foundation.h>
/**
* Protocol that is required to be implemented for all mapped
* objects.
*/
@protocol STMMappedObject <NSObject>
/**
* The name of the schema as defined by the schema configuration with stackmob.
* All stackmob mapped objects must implement this protocol.
*/
- (NSString *) schemaName;
/** The name of the property that is the primary key */
- (NSString *) primaryKeyPropertyName;
@optional
/**
* All relationship classes must be defined via this method.
* Returning nil will result in the relationship not being mapped.
* Example:
* For the propertyName "addresses", you would return a class
* like "AddressModel".
*
*/
- (Class) classForRelationshipPropertyName:(NSString *)propertyName;
@end
@interface UserModel : NSObject <STMMappedObject>
@property (nonatomic, strong) NSString *userName;
@property (nonatomic, strong) NSNumber *lastModDate;
@property (nonatomic, strong) NSNumber *createDate;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *password;
@property (nonatomic, strong) NSString *email;
@property (nonatomic, strong) NSArray *groups;
@end
@implementation UserModel
@synthesize email;
@synthesize name;
@synthesize password;
@synthesize lastModDate;
@synthesize createDate;
@synthesize userName;
@synthesize groups;
- (NSString *) schemaName
{
return @"user";
}
- (NSString *) primaryKeyPropertyName
{
return @"userName";
}
- (BOOL) isEqual:(id)object
{
return ([email isEqualToString:[object email]] &&
[name isEqualToString:[object name]] &&
[lastModDate isEqual:[object lastModDate]] &&
[userName isEqualToString:[object userName]]);
}
- (Class) classForRelationshipPropertyName:(NSString *)propertyName
{
if([propertyName isEqualToString:@"groups"])
{
return [GroupModel class];
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment