Skip to content

Instantly share code, notes, and snippets.

View gonzalezreal's full-sized avatar

Guille Gonzalez gonzalezreal

View GitHub Profile
- (void)importTweets:(NSArray *)tweets
{
for (NSDictionary *tweetDictionary in tweets) {
[TGRTweet importFromDictionary:tweetDictionary inManagedObjectContext:self.managedObjectContext];
}
NSError *error = nil;
BOOL succeeded = [self.managedObjectContext save:&error];
if (!succeeded) {
- (SLRequest *)requestForOldTweets
{
TGRTweet *firstTweet = [TGRTweet firstTweetInManagedObjectContext:self.managedObjectContext];
if (!firstTweet) {
NSLog(@"requestForOldTweets shouldn't be called when the cache is empty");
return nil;
}
NSNumber *maxIdentifier = [NSNumber numberWithLongLong:[firstTweet.identifier longLongValue] - 1];
- (SLRequest *)requestForNewTweets
{
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"include_rts"] = @"true";
TGRTweet *lastTweet = [TGRTweet lastTweetInManagedObjectContext:self.managedObjectContext];
if (lastTweet) {
parameters[@"since_id"] = [lastTweet.identifier stringValue];
}
static NSString * const kHomeTimelineURL = @"https://api.twitter.com/1.1/statuses/home_timeline.json";
+ (NSDate *)dateFromTwitterDate:(NSString *)dateString
{
static dispatch_once_t onceToken;
static NSDateFormatter *dateFormatter;
dispatch_once(&onceToken, ^{
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:@"eee MMM dd HH:mm:ss ZZZZ yyyy"];
- (void)importValuesFromDictionary:(NSDictionary *)dictionary
{
self.identifier = dictionary[@"id"];
NSDictionary *retweetedStatus = dictionary[@"retweeted_status"];
NSString *dateString = nil;
NSDictionary *userDictionary = nil;
NSDictionary *retweetedByDictionary = nil;
+ (id)lastTweetInManagedObjectContext:(NSManagedObjectContext *)context
{
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"identifier"
ascending:NO];
NSFetchRequest *fetchRequest = [self fetchRequest];
[fetchRequest setSortDescriptors:@[sortDescriptor]];
[fetchRequest setFetchLimit:1];
return [[context executeFetchRequest:fetchRequest error:NULL] lastObject];
+ (id)firstTweetInManagedObjectContext:(NSManagedObjectContext *)context
{
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"identifier"
ascending:YES];
NSFetchRequest *fetchRequest = [self fetchRequest];
[fetchRequest setSortDescriptors:@[sortDescriptor]];
[fetchRequest setFetchLimit:1];
return [[context executeFetchRequest:fetchRequest error:NULL] lastObject];
+ (NSFetchRequest *)fetchRequestForAllTweets
{
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"identifier"
ascending:NO];
NSFetchRequest *fetchRequest = [self fetchRequest];
[fetchRequest setSortDescriptors:@[sortDescriptor]];
return fetchRequest;
}
+ (NSFetchRequest *)fetchRequestForAllTweets;
+ (id)firstTweetInManagedObjectContext:(NSManagedObjectContext *)context;
+ (id)lastTweetInManagedObjectContext:(NSManagedObjectContext *)context;