Create RKTwitterCoreData project. It's a clone of the RKTwitter example, but using core data.

This commit is contained in:
Jeremy Ellison
2010-10-05 15:05:57 -04:00
parent 5dc77ed536
commit 7c0768a004
20 changed files with 887 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
//
// RKTStatus.h
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright 2010 Two Toasters. All rights reserved.
//
#import <RestKit/RestKit.h>
#import "RKTUser.h"
#import <RestKit/CoreData/CoreData.h>
@interface RKTStatus : RKManagedObject {
}
/**
* The unique ID of this Status
*/
@property (nonatomic, retain) NSNumber* statusID;
/**
* Timestamp the Status was sent
*/
@property (nonatomic, retain) NSDate* createdAt;
/**
* Text of the Status
*/
@property (nonatomic, retain) NSString* text;
/**
* String version of the URL associated with the Status
*/
@property (nonatomic, retain) NSString* urlString;
/**
* The screen name of the User this Status was in response to
*/
@property (nonatomic, retain) NSString* inReplyToScreenName;
/**
* Is this status a favorite?
*/
@property (nonatomic, assign) BOOL isFavorited;
/**
* The User who posted this status
*/
@property (nonatomic, retain) RKTUser* user;
@end

View File

@@ -0,0 +1,48 @@
//
// RKTStatus.m
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright 2010 Two Toasters. All rights reserved.
//
#import "RKTStatus.h"
@implementation RKTStatus
@dynamic statusID;
@dynamic createdAt;
@dynamic text;
@dynamic urlString;
@dynamic inReplyToScreenName;
@dynamic isFavorited;
@dynamic user;
#pragma mark RKObjectMappable methods
+ (NSDictionary*)elementToPropertyMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"id", @"statusID",
@"created_at", @"createdAt",
@"text", @"text",
@"url", @"urlString",
@"in_reply_to_screen_name", @"inReplyToScreenName",
@"favorited", @"isFavorited",
nil];
}
+ (NSDictionary*)elementToRelationshipMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"user", @"user",
nil];
}
+ (NSString*)primaryKey {
return @"statusID";
}
- (NSString*)description {
return [NSString stringWithFormat:@"%@ (ID: %@)", self.text, self.statusID];
}
@end

View File

@@ -0,0 +1,19 @@
//
// RKTUser.h
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright 2010 Two Toasters. All rights reserved.
//
#import <RestKit/RestKit.h>
#import <RestKit/CoreData/CoreData.h>
@interface RKTUser : RKManagedObject {
}
@property (nonatomic, retain) NSNumber* userID;
@property (nonatomic, retain) NSString* name;
@property (nonatomic, retain) NSString* screenName;
@end

View File

@@ -0,0 +1,31 @@
//
// RKTUser.m
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright 2010 Two Toasters. All rights reserved.
//
#import "RKTUser.h"
@implementation RKTUser
@dynamic userID;
@dynamic name;
@dynamic screenName;
#pragma mark RKObjectMappable methods
+ (NSDictionary*)elementToPropertyMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"id", @"userID",
@"screen_name", @"screenName",
@"name", @"name",
nil];
}
+ (NSString*)primaryKey {
return @"userID";
}
@end

View File

@@ -0,0 +1,16 @@
//
// RKTwitterAppDelegate.h
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright Two Toasters 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RKTwitterAppDelegate : NSObject <UIApplicationDelegate> {
}
@end

View File

@@ -0,0 +1,48 @@
//
// RKTwitterAppDelegate.m
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright Two Toasters 2010. All rights reserved.
//
#import <RestKit/RestKit.h>
#import "RKTwitterAppDelegate.h"
#import "RKTwitterViewController.h"
#import "RKTStatus.h"
#import "RKTUser.h"
@implementation RKTwitterAppDelegate
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize RestKit
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://twitter.com"];
RKObjectMapper* mapper = objectManager.mapper;
// Initialize object store
objectManager.objectStore = [[[RKManagedObjectStore alloc] initWithStoreFilename:@"RKTwitterData.sqlite"] autorelease];
// Add our element to object mappings
[mapper registerClass:[RKTUser class] forElementNamed:@"user"];
[mapper registerClass:[RKTStatus class] forElementNamed:@"status"];
// Create Window and View Controllers
RKTwitterViewController* viewController = [[[RKTwitterViewController alloc] initWithNibName:nil bundle:nil] autorelease];
UINavigationController* controller = [[UINavigationController alloc] initWithRootViewController:viewController];
UIWindow* window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[window addSubview:controller.view];
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[super dealloc];
}
@end

View File

@@ -0,0 +1,17 @@
//
// RKTwitterViewController.h
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright Two Toasters 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <RestKit/RestKit.h>
@interface RKTwitterViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, RKObjectLoaderDelegate> {
UITableView* _tableView;
NSArray* _statuses;
}
@end

View File

@@ -0,0 +1,120 @@
//
// RKTwitterViewController.m
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright Two Toasters 2010. All rights reserved.
//
#import "RKTwitterViewController.h"
#import "RKTStatus.h"
@implementation RKTwitterViewController
- (void)loadView {
[super loadView];
// Setup View and Table View
self.title = @"Two Toasters Tweets";
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BG.png"]] autorelease];
imageView.frame = CGRectOffset(imageView.frame, 0, -64);
[self.view insertSubview:imageView atIndex:0];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
_tableView.backgroundColor = [UIColor clearColor];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
UIBarButtonItem* reloadItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadButtonWasPressed:)] autorelease];
[self.navigationItem setRightBarButtonItem:reloadItem];
// Load statuses from core data
[self loadObjectsFromDataStore];
}
- (void)dealloc {
[_tableView release];
[_statuses release];
[super dealloc];
}
- (void)loadObjectsFromDataStore {
[_statuses release];
_statuses = [[RKTStatus allObjects] retain];
NSFetchRequest* request = [RKTStatus request];
NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey:@"statusID" ascending:NO];
[request setSortDescriptors:[NSArray arrayWithObject:descriptor]];
[[RKTStatus objectsWithRequest:request] retain];
}
- (void)reloadButtonWasPressed:(id)sender {
// Load the object model via RestKit
// Retain the object loader that is returned so that it is not dealloc'd after the request is sent.
RKObjectManager* objectManager = [RKObjectManager globalManager];
[[objectManager loadObjectsAtResourcePath:@"/status/user_timeline/twotoasters.json" objectClass:[RKTStatus class] delegate:self] retain];
}
#pragma mark RKObjectLoaderDelegate methods
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"LastUpdatedAt"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"Loaded statuses: %@", objects);
[self loadObjectsFromDataStore];
[_tableView reloadData];
[objectLoader release];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert show];
NSLog(@"Hit error: %@", error);
[objectLoader release];
}
#pragma mark UITableViewDelegate methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize size = [[[_statuses objectAtIndex:indexPath.row] text] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, 9000)];
return size.height + 10;
}
#pragma mark UITableViewDataSource methods
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [_statuses count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSDate* lastUpdatedAt = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastUpdatedAt"];
NSString* dateString = [NSDateFormatter localizedStringFromDate:lastUpdatedAt dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterMediumStyle];
if (nil == dateString) {
dateString = @"Never";
}
return [NSString stringWithFormat:@"Last Load: %@", dateString];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* reuseIdentifier = @"Tweet Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (nil == cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.numberOfLines = 0;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listbg.png"]];
}
cell.textLabel.text = [[_statuses objectAtIndex:indexPath.row] text];
return cell;
}
@end