Skip to content

Instantly share code, notes, and snippets.

@stevemoser
Created August 24, 2012 02:01
Show Gist options
  • Save stevemoser/3444690 to your computer and use it in GitHub Desktop.
Save stevemoser/3444690 to your computer and use it in GitHub Desktop.
RestKit post 3
#import <RestKit/RestKit.h>
@class SMArticle
@interface SMArticleDataController : NSObject
- (void)loadArticlesWithOnLoad:(RKObjectLoaderDidLoadObjectsBlock)loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock;
- (int)articleCount;
- (SMArticle*)articleAtIndex:(int)index;
- (int)indexForArticle:(SMArticle*)Article;
- (void)saveArticle:(SMArticle*)article WithOnLoad:(RKObjectLoaderDidLoadObjectsBlock)loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock;
- (void)deleteArticle:(SMArticle*)article onError:(RKRequestDidFailLoadWithErrorBlock)failBlock;
@end
#import "SMArticleDataController.h"
#import "SMArticle.h"
@interface SMArticlesDataController ()
@property (nonatomic,retain) NSMutableArray* articles;
@end
@implementation SMArticlesDataSource
- (id)init
{
if ((self = [super init]))
{
[self setArticles:[[NSMutableArray alloc] init]];
}
return self;
}
- (int)articleCount
{
return self.articles.count;
}
- (SMArticle*)articleAtIndex:(int)index
{
if ([self.articles count] > 0) {
return [self.articles objectAtIndex:index];
}
return nil;
}
- (int)indexForArticle:(SMArticle*)article
{
return [[self articles] indexOfObject: article];
}
- (void)addArticle:(SMArticle*)article
{
[self.articles addObject: article];
}
#pragma mark - API Calls
- (void)loadArticlesWithOnLoad:(RKObjectLoaderDidLoadObjectsBlock)loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock
{
__weak MMArticleDataController *me = self;
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager sendObject:nil toResourcePath:@"api/v1/articles.json" usingBlock:^(RKObjectLoader* loader) {
loader.method = RKRequestMethodGET;
loader.serializationMIMEType = RKMIMETypeJSON;
loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[SMArticle class]];
loader.onDidLoadObjects = ^(NSArray *objects) {
if (me != nil) {
[me setArticles:[objects mutableCopy]];
loadBlock(objects);
}
};
loader.onDidFailWithError = failBlock;
loader.onDidFailLoadWithError = failBlock;
}];
}
- (void)saveArticle:(SMArticle*)article WithOnLoad:(RKObjectLoaderDidLoadObjectsBlock)loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock
{
if ([self.articles containsObject:article]) {
[self putArticle:article WithOnLoad:loadBlock onError:failBlock];
} else {
[self postArticle:article WithOnLoad:loadBlock onError:failBlock];
}
}
- (void)putArticle:(SMArticle*)article WithOnLoad:(RKObjectLoaderDidLoadObjectsBlock)loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock
{
__weak MMArticleDataController *me = self;
RKObjectManager* objectManager = [RKObjectManager sharedManager];
[objectManager putObject:article usingBlock:^(RKObjectLoader* loader) {
loader.serializationMIMEType = RKMIMETypeJSON;
loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[SMArticle class]];
loader.onDidLoadObjects = ^(NSArray *objects) {
if (me != nil) {
[me.articles removeObject:article];
[me addArticle:[objects lastObject]];
loadBlock(objects);
}
};
loader.onDidFailWithError = failBlock;
loader.onDidFailLoadWithError = failBlock;
}];
}
- (void)postArticle:(SMArticle*)article WithOnLoad:(RKObjectLoaderDidLoadObjectsBlock)loadBlock
onError:(RKRequestDidFailLoadWithErrorBlock)failBlock
{
__weak MMArticleDataController *me = self;
RKObjectManager* objectManager = [RKObjectManager sharedManager];
[objectManager postObject:article usingBlock:^(RKObjectLoader* loader) {
loader.serializationMIMEType = RKMIMETypeJSON;
loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[SMArticle class]];
loader.onDidLoadObjects = ^(NSArray *objects) {
if (me != nil) {
[me addArticle:[objects lastObject]];
loadBlock(objects);
}
};
loader.onDidFailWithError = failBlock;
loader.onDidFailLoadWithError = failBlock;
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment