Refresh Twitter examples and port to RKEntityIdentifier

This commit is contained in:
Blake Watters
2012-11-28 23:25:48 -05:00
parent 1318edd082
commit b6d4412415
13 changed files with 53 additions and 43 deletions

View File

@@ -1,5 +1,5 @@
//
// RKTStatus.h
// RKTweet.h
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
@@ -8,7 +8,7 @@
#import "RKTUser.h"
@interface RKTStatus : NSObject
@interface RKTweet : NSObject
/**
* The unique ID of this Status

View File

@@ -1,14 +1,14 @@
//
// RKTStatus.m
// RKTweet.m
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//
#import "RKTStatus.h"
#import "RKTweet.h"
@implementation RKTStatus
@implementation RKTweet
- (NSString *)description
{

View File

@@ -9,7 +9,7 @@
#import <RestKit/RestKit.h>
#import "RKTwitterAppDelegate.h"
#import "RKTwitterViewController.h"
#import "RKTStatus.h"
#import "RKTweet.h"
#import "RKTUser.h"
@implementation RKTwitterAppDelegate
@@ -43,7 +43,7 @@
@"name" : @"name"
}];
RKObjectMapping *statusMapping = [RKObjectMapping mappingForClass:[RKTStatus class]];
RKObjectMapping *statusMapping = [RKObjectMapping mappingForClass:[RKTweet class]];
[statusMapping addAttributeMappingsFromDictionary:@{
@"id" : @"statusID",
@"created_at" : @"createdAt",

View File

@@ -7,7 +7,7 @@
//
#import "RKTwitterViewController.h"
#import "RKTStatus.h"
#import "RKTweet.h"
@interface RKTwitterViewController (Private)
- (void)loadData;
@@ -92,7 +92,7 @@
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listbg.png"]];
}
RKTStatus* status = [_statuses objectAtIndex:indexPath.row];
RKTweet* status = [_statuses objectAtIndex:indexPath.row];
cell.textLabel.text = [status text];
cell.detailTextLabel.text = status.user.screenName;
return cell;

View File

@@ -16,7 +16,7 @@
250CA69B147D8FD30047D347 /* libRestKit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 25160FB31456E8A30060A5C5 /* libRestKit.a */; };
250CA69C147D8FFD0047D347 /* CoreData.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2538E864123424F000ACB5D7 /* CoreData.framework */; };
2538E811123419CA00ACB5D7 /* RKTUser.m in Sources */ = {isa = PBXBuildFile; fileRef = 2538E810123419CA00ACB5D7 /* RKTUser.m */; };
2538E814123419EC00ACB5D7 /* RKTStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 2538E813123419EC00ACB5D7 /* RKTStatus.m */; };
2538E814123419EC00ACB5D7 /* RKTweet.m in Sources */ = {isa = PBXBuildFile; fileRef = 2538E813123419EC00ACB5D7 /* RKTweet.m */; };
2538E8671234250100ACB5D7 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2538E8661234250100ACB5D7 /* SystemConfiguration.framework */; };
25BE936614F96729008BC1C0 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 25BE936514F96729008BC1C0 /* QuartzCore.framework */; };
288765A50DF7441C002DB57D /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 288765A40DF7441C002DB57D /* CoreGraphics.framework */; };
@@ -81,8 +81,8 @@
250CA699147D8FCC0047D347 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
2538E80F123419CA00ACB5D7 /* RKTUser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKTUser.h; sourceTree = "<group>"; };
2538E810123419CA00ACB5D7 /* RKTUser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKTUser.m; sourceTree = "<group>"; };
2538E812123419EC00ACB5D7 /* RKTStatus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKTStatus.h; sourceTree = "<group>"; };
2538E813123419EC00ACB5D7 /* RKTStatus.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKTStatus.m; sourceTree = "<group>"; };
2538E812123419EC00ACB5D7 /* RKTweet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKTweet.h; sourceTree = "<group>"; };
2538E813123419EC00ACB5D7 /* RKTweet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKTweet.m; sourceTree = "<group>"; };
2538E864123424F000ACB5D7 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; };
2538E8661234250100ACB5D7 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
25BE936514F96729008BC1C0 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
@@ -134,8 +134,8 @@
28D7ACF70DDB3853001CB0EB /* RKTwitterViewController.m */,
2538E80F123419CA00ACB5D7 /* RKTUser.h */,
2538E810123419CA00ACB5D7 /* RKTUser.m */,
2538E812123419EC00ACB5D7 /* RKTStatus.h */,
2538E813123419EC00ACB5D7 /* RKTStatus.m */,
2538E812123419EC00ACB5D7 /* RKTweet.h */,
2538E813123419EC00ACB5D7 /* RKTweet.m */,
);
path = Classes;
sourceTree = "<group>";
@@ -324,7 +324,7 @@
1D3623260D0F684500981E51 /* RKTwitterAppDelegate.m in Sources */,
28D7ACF80DDB3853001CB0EB /* RKTwitterViewController.m in Sources */,
2538E811123419CA00ACB5D7 /* RKTUser.m in Sources */,
2538E814123419EC00ACB5D7 /* RKTStatus.m in Sources */,
2538E814123419EC00ACB5D7 /* RKTweet.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@@ -1,5 +1,5 @@
//
// RKTStatus.h
// RKTweet.h
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
@@ -9,7 +9,7 @@
#import <RestKit/RestKit.h>
#import <RestKit/CoreData.h>
@interface RKTStatus : NSManagedObject
@interface RKTweet : NSManagedObject
/**
The unique ID of this Status

View File

@@ -1,14 +1,14 @@
//
// RKTStatus.m
// RKTweet.m
// RKTwitter
//
// Created by Blake Watters on 9/5/10.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//
#import "RKTStatus.h"
#import "RKTweet.h"
@implementation RKTStatus
@implementation RKTweet
@dynamic statusID;
@dynamic createdAt;

View File

@@ -10,7 +10,7 @@
#import <RestKit/CoreData.h>
#import "RKTwitterAppDelegate.h"
#import "RKTwitterViewController.h"
#import "RKTStatus.h"
#import "RKTweet.h"
@implementation RKTwitterAppDelegate
@@ -39,8 +39,8 @@
name. This allows us to map back Twitter user objects directly onto NSManagedObject instances --
there is no backing model class!
*/
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"RKTUser" inManagedObjectStore:managedObjectStore];
userMapping.primaryKeyAttribute = @"userID";
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
[userMapping setEntityIdentifier:[RKEntityIdentifier identifierWithEntityName:@"User" attributes:@[ @"userID" ] inManagedObjectStore:managedObjectStore]];
[userMapping addAttributeMappingsFromDictionary:@{
@"id": @"userID",
@"screen_name": @"screenName",
@@ -48,9 +48,9 @@
// If source and destination key path are the same, we can simply add a string to the array
[userMapping addAttributeMappingsFromArray:@[ @"name" ]];
RKEntityMapping *statusMapping = [RKEntityMapping mappingForEntityForName:@"RKTStatus" inManagedObjectStore:managedObjectStore];
statusMapping.primaryKeyAttribute = @"statusID";
[statusMapping addAttributeMappingsFromDictionary:@{
RKEntityMapping *tweetMapping = [RKEntityMapping mappingForEntityForName:@"Tweet" inManagedObjectStore:managedObjectStore];
[tweetMapping setEntityIdentifier:[RKEntityIdentifier identifierWithEntityName:@"Tweet" attributes:@[ @"statusID" ] inManagedObjectStore:managedObjectStore]];
[tweetMapping addAttributeMappingsFromDictionary:@{
@"id": @"statusID",
@"created_at": @"createdAt",
@"text": @"text",
@@ -58,14 +58,14 @@
@"in_reply_to_screen_name": @"inReplyToScreenName",
@"favorited": @"isFavorited",
}];
[statusMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"user" toKeyPath:@"user" withMapping:userMapping]];
[tweetMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"user" toKeyPath:@"user" withMapping:userMapping]];
// 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];
// Register our mappings with the provider
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:statusMapping
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:tweetMapping
pathPattern:@"/status/user_timeline/:username"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
@@ -89,7 +89,7 @@
NSString *seedStorePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"RKSeedDatabase.sqlite"];
RKManagedObjectImporter *importer = [[RKManagedObjectImporter alloc] initWithManagedObjectModel:managedObjectModel storePath:seedStorePath];
[importer importObjectsFromItemAtPath:[[NSBundle mainBundle] pathForResource:@"restkit" ofType:@"json"]
withMapping:statusMapping
withMapping:tweetMapping
keyPath:nil
error:&error];
[importer importObjectsFromItemAtPath:[[NSBundle mainBundle] pathForResource:@"users" ofType:@"json"]
@@ -103,7 +103,8 @@
RKLogError(@"Failed to finish import and save seed database due to error: %@", error);
}
exit(0);
// Clear out the root view controller
[self.window setRootViewController:[UIViewController new]];
#else
/**
Complete Core Data stack initialization

View File

@@ -7,7 +7,7 @@
//
#import "RKTwitterViewController.h"
#import "RKTStatus.h"
#import "RKTweet.h"
static void RKTwitterShowAlertWithError(NSError *error)
{
@@ -18,7 +18,7 @@ static void RKTwitterShowAlertWithError(NSError *error)
[alert show];
}
@interface RKTwitterViewController () <UITableViewDelegate, UITableViewDataSource>
@interface RKTwitterViewController () <UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate>
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@end
@@ -35,7 +35,7 @@ static void RKTwitterShowAlertWithError(NSError *error)
// Setup View and Table View
self.title = @"RestKit Tweets";
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"RKTStatus"];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Tweet"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:NO];
fetchRequest.sortDescriptors = @[descriptor];
NSError *error = nil;
@@ -45,7 +45,9 @@ static void RKTwitterShowAlertWithError(NSError *error)
managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self.fetchedResultsController setDelegate:self];
BOOL fetchSuccessful = [self.fetchedResultsController performFetch:&error];
NSAssert([[self.fetchedResultsController fetchedObjects] count], @"Seeding didn't work...");
if (! fetchSuccessful) {
RKTwitterShowAlertWithError(error);
}
@@ -79,7 +81,7 @@ static void RKTwitterShowAlertWithError(NSError *error)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
RKTStatus *status = [self.fetchedResultsController objectAtIndexPath:indexPath];
RKTweet *status = [self.fetchedResultsController objectAtIndexPath:indexPath];
CGSize size = [[status text] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, 9000)];
return size.height + 10;
}
@@ -113,9 +115,16 @@ static void RKTwitterShowAlertWithError(NSError *error)
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listbg.png"]];
}
RKTStatus *status = [self.fetchedResultsController objectAtIndexPath:indexPath];
RKTweet *status = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = status.text;
return cell;
}
#pragma mark NSFetchedResultsControllerDelegate methods
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView reloadData];
}
@end

View File

@@ -18,7 +18,7 @@
250CA6D0147D90C50047D347 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 250CA6CF147D90C50047D347 /* Security.framework */; };
252E157D16397DCA000B729E /* MainStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2536AF3B16397A0200AD803D /* MainStoryboard.storyboard */; };
2536AF3C16397A0200AD803D /* MainStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2536AF3B16397A0200AD803D /* MainStoryboard.storyboard */; };
2538E814123419EC00ACB5D7 /* RKTStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 2538E813123419EC00ACB5D7 /* RKTStatus.m */; };
2538E814123419EC00ACB5D7 /* RKTweet.m in Sources */ = {isa = PBXBuildFile; fileRef = 2538E813123419EC00ACB5D7 /* RKTweet.m */; };
2538E865123424F000ACB5D7 /* CoreData.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2538E864123424F000ACB5D7 /* CoreData.framework */; };
2538E8671234250100ACB5D7 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2538E8661234250100ACB5D7 /* SystemConfiguration.framework */; };
25F2A1791322D59400A33DE4 /* listbg.png in Resources */ = {isa = PBXBuildFile; fileRef = 3F3CE3FA125B9A6E0083FDCB /* listbg.png */; };
@@ -30,7 +30,7 @@
25F2A1801322D59400A33DE4 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; };
25F2A1811322D59400A33DE4 /* RKTwitterAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1D3623250D0F684500981E51 /* RKTwitterAppDelegate.m */; };
25F2A1821322D59400A33DE4 /* RKTwitterViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 28D7ACF70DDB3853001CB0EB /* RKTwitterViewController.m */; };
25F2A1841322D59400A33DE4 /* RKTStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 2538E813123419EC00ACB5D7 /* RKTStatus.m */; };
25F2A1841322D59400A33DE4 /* RKTweet.m in Sources */ = {isa = PBXBuildFile; fileRef = 2538E813123419EC00ACB5D7 /* RKTweet.m */; };
25F2A1851322D59400A33DE4 /* RKTwitterCoreData.xcdatamodel in Sources */ = {isa = PBXBuildFile; fileRef = 3F94E0C6125BA8C0001E8585 /* RKTwitterCoreData.xcdatamodel */; };
25F2A1871322D59400A33DE4 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
25F2A1881322D59400A33DE4 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */; };
@@ -111,8 +111,8 @@
250CA6CF147D90C50047D347 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
2536AF3B16397A0200AD803D /* MainStoryboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = MainStoryboard.storyboard; sourceTree = "<group>"; };
2538E7FF123417E500ACB5D7 /* RestKit.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RestKit.xcodeproj; path = ../../RestKit.xcodeproj; sourceTree = SOURCE_ROOT; };
2538E812123419EC00ACB5D7 /* RKTStatus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKTStatus.h; sourceTree = "<group>"; };
2538E813123419EC00ACB5D7 /* RKTStatus.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKTStatus.m; sourceTree = "<group>"; };
2538E812123419EC00ACB5D7 /* RKTweet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKTweet.h; sourceTree = "<group>"; };
2538E813123419EC00ACB5D7 /* RKTweet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKTweet.m; sourceTree = "<group>"; };
2538E864123424F000ACB5D7 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; };
2538E8661234250100ACB5D7 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
25F2A1961322D59400A33DE4 /* Generate Seed Database.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Generate Seed Database.app"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -184,8 +184,8 @@
1D3623250D0F684500981E51 /* RKTwitterAppDelegate.m */,
28D7ACF60DDB3853001CB0EB /* RKTwitterViewController.h */,
28D7ACF70DDB3853001CB0EB /* RKTwitterViewController.m */,
2538E812123419EC00ACB5D7 /* RKTStatus.h */,
2538E813123419EC00ACB5D7 /* RKTStatus.m */,
2538E812123419EC00ACB5D7 /* RKTweet.h */,
2538E813123419EC00ACB5D7 /* RKTweet.m */,
);
path = Classes;
sourceTree = "<group>";
@@ -414,7 +414,7 @@
1D60589B0D05DD56006BFB54 /* main.m in Sources */,
1D3623260D0F684500981E51 /* RKTwitterAppDelegate.m in Sources */,
28D7ACF80DDB3853001CB0EB /* RKTwitterViewController.m in Sources */,
2538E814123419EC00ACB5D7 /* RKTStatus.m in Sources */,
2538E814123419EC00ACB5D7 /* RKTweet.m in Sources */,
3F94E0C7125BA8C0001E8585 /* RKTwitterCoreData.xcdatamodel in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -426,7 +426,7 @@
25F2A1801322D59400A33DE4 /* main.m in Sources */,
25F2A1811322D59400A33DE4 /* RKTwitterAppDelegate.m in Sources */,
25F2A1821322D59400A33DE4 /* RKTwitterViewController.m in Sources */,
25F2A1841322D59400A33DE4 /* RKTStatus.m in Sources */,
25F2A1841322D59400A33DE4 /* RKTweet.m in Sources */,
25F2A1851322D59400A33DE4 /* RKTwitterCoreData.xcdatamodel in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;