Merge Twitter example code fixes from mdd; closes #927

This commit is contained in:
Blake Watters
2012-09-10 14:54:53 -04:00
parent 743592b1ff
commit ba4301c43d
3 changed files with 60 additions and 50 deletions

View File

@@ -22,39 +22,51 @@
RKLogConfigureByName("RestKit/Network*", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
// Initialize RestKit
//let AFNetworking manage the activity indicator
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
// Initialize HTTPClient
NSURL *baseURL = [NSURL URLWithString:@"http://twitter.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
// Enable automatic network activity indicator management
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
//we want to work with JSON-Data
[client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];
// Initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
// Setup our object mappings
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[RKTUser class]];
[userMapping mapKeyPath:@"id" toAttribute:@"userID"];
[userMapping mapKeyPath:@"screen_name" toAttribute:@"screenName"];
[userMapping mapAttributes:@"name", nil];
[userMapping addAttributeMappingsFromDictionary:@{
@"id" : @"userID",
@"screen_name" : @"screenName",
@"name" : @"name"
}];
RKObjectMapping *statusMapping = [RKObjectMapping mappingForClass:[RKTStatus class]];
[statusMapping mapKeyPathsToAttributes:@"id", @"statusID",
@"created_at", @"createdAt",
@"text", @"text",
@"url", @"urlString",
@"in_reply_to_screen_name", @"inReplyToScreenName",
@"favorited", @"isFavorited",
nil];
[statusMapping mapRelationship:@"user" withMapping:userMapping];
[statusMapping addAttributeMappingsFromDictionary:@{
@"id" : @"statusID",
@"created_at" : @"createdAt",
@"text" : @"text",
@"url" : @"urlString",
@"in_reply_to_screen_name" : @"inReplyToScreenName",
@"favorited" : @"isFavorited",
}];
RKRelationshipMapping* relationShipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"user"
toKeyPath:@"user"
withMapping:userMapping];
[statusMapping addPropertyMapping:relationShipMapping];
// Update date format so that we can parse Twitter dates properly
// Wed Sep 29 15:31:08 +0000 2010
[RKObjectMapping addDefaultDateFormatterForString:@"E MMM d HH:mm:ss Z y" inTimeZone:nil];
// Uncomment these lines to use XML, comment it to use JSON
// objectManager.acceptMIMEType = RKMIMETypeXML;
// statusMapping.rootKeyPath = @"statuses.status";
// Register our mappings with the provider using a resource path pattern
[objectManager.mappingProvider setObjectMapping:statusMapping forResourcePathPattern:@"/status/user_timeline/:username"];
// Register our mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:statusMapping
pathPattern:@"/status/user_timeline/:username"
keyPath:nil
statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
// Create Window and View Controllers
RKTwitterViewController *viewController = [[[RKTwitterViewController alloc] initWithNibName:nil bundle:nil] autorelease];

View File

@@ -9,7 +9,7 @@
#import <UIKit/UIKit.h>
#import <RestKit/RestKit.h>
@interface RKTwitterViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, RKObjectLoaderDelegate> {
@interface RKTwitterViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *_tableView;
NSArray *_statuses;
}

View File

@@ -19,8 +19,26 @@
{
// Load the object model via RestKit
RKObjectManager *objectManager = [RKObjectManager sharedManager];
objectManager.client.baseURL = [RKURL URLWithString:@"http://www.twitter.com"];
[objectManager loadObjectsAtResourcePath:@"/status/user_timeline/RestKit" delegate:self];
[objectManager getObjectsAtPath:@"/status/user_timeline/RestKit"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSArray* statuses = [mappingResult asCollection];
NSLog(@"Loaded statuses: %@", statuses);
[_statuses release];
_statuses = [statuses retain];
if(self.isViewLoaded)
[_tableView reloadData];
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease];
[alert show];
NSLog(@"Hit error: %@", error);
}];
}
- (void)loadView
@@ -55,34 +73,12 @@
[super dealloc];
}
#pragma mark RKObjectLoaderDelegate methods
- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response
{
NSLog(@"Loaded payload: %@", [response bodyAsString]);
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
NSLog(@"Loaded statuses: %@", objects);
[_statuses release];
_statuses = [objects retain];
[_tableView reloadData];
}
- (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);
}
#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;
return size.height + 30;
}
#pragma mark UITableViewDataSource methods
@@ -97,13 +93,15 @@
NSString *reuseIdentifier = @"Tweet Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (nil == cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 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];
RKTStatus* status = [_statuses objectAtIndex:indexPath.row];
cell.textLabel.text = [status text];
cell.detailTextLabel.text = status.user.screenName;
return cell;
}