Skip to content

Instantly share code, notes, and snippets.

@jhosteny
Created October 22, 2012 17:12
Show Gist options
  • Save jhosteny/3932673 to your computer and use it in GitHub Desktop.
Save jhosteny/3932673 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import <RestKit/CoreData.h>
@interface BRAccountItem : NSManagedObject
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSDate * dateCreated;
@property (nonatomic, retain) NSDate * dateUpdated;
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * phone;
@property (nonatomic, retain) NSString * zipcode;
@end
#import "BRAccountItem.h"
@implementation BRAccountItem
@dynamic email;
@dynamic dateCreated;
@dynamic dateUpdated;
@dynamic id;
@dynamic phone;
@dynamic zipcode;
@end
- (void)loadObjectsFromDataStore {
NSFetchRequest *request = [BRAccountItem fetchRequest];
// NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"dateCreated" ascending:NO];
// [request setSortDescriptors:[NSArray arrayWithObject:descriptor]];
// [self setAccount:[BRAccountItem objectWithFetchRequest:request]];
[self setAccount:[BRAccountItem findByPrimaryKey:[NSNumber numberWithInt:1]]];
NSLog(@"loaded %@, id %@", self.account.email, self.account.id);
[self setAccount:[BRAccountItem object]];
self.account.id = [NSNumber numberWithInt:1];
#if 0
[[RKObjectManager sharedManager] getObject:[self account] delegate:self];
#else
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/accounts/1" delegate:self];
#endif
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects {
NSArray *ary = [BRAccountItem findAll];
for (BRAccountItem *item in ary) {
NSLog(@"loaded %@, id %@", item.email, item.id);
}
}
#pragma mark - RKObjectLoaderDelegate
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObject:(id)object {
NSLog(@"Did load account %@", [[self account] email]);
[[self emailLabel] setText:[[self account] email]];
[[self view] setNeedsDisplay];
[[self refreshControl] endRefreshing];
NSArray *ary = [BRAccountItem findAll];
for (BRAccountItem *item in ary) {
NSLog(@"loaded %@, id %@", item.email, item.id);
}
}
- (void)setupRestKit {
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURLString:BRBaseURL];
[objectManager setObjectStore:[RKManagedObjectStore objectStoreWithStoreFilename:@"BestRate.sqlite"
usingSeedDatabaseName:nil
managedObjectModel:nil
delegate:self]];
// TODO: to get rid of duplicates
// [[objectManager objectStore] setCacheStrategy:[RKFetchRequestManagedObjectCache new]];
RKManagedObjectMapping *accountMapping =
[RKManagedObjectMapping mappingForClass:[BRAccountItem class]
inManagedObjectStore:[objectManager objectStore]];
[accountMapping setPrimaryKeyAttribute:@"id"];
[accountMapping mapKeyPath:@"id"
toAttribute:@"id"];
[accountMapping mapKeyPath:@"created_at"
toAttribute:@"dateCreated"];
[accountMapping mapKeyPath:@"updated_at"
toAttribute:@"dateUpdated"];
[accountMapping mapKeyPath:@"email"
toAttribute:@"email"];
[accountMapping mapKeyPath:@"mobile_phone"
toAttribute:@"phone"];
[accountMapping mapKeyPath:@"zipcode"
toAttribute:@"zipcode"];
[[objectManager mappingProvider] setObjectMapping:accountMapping forKeyPath:@"account"];
// Setup routes
[[objectManager router] routeClass:[BRAccountItem class]
toResourcePath:@"/accounts/:id"];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment