Skip to content

Instantly share code, notes, and snippets.

View gonzalezreal's full-sized avatar

Guille Gonzalez gonzalezreal

View GitHub Profile
#import <Foundation/Foundation.h>
#import <Accounts/Accounts.h>
#import <CoreData/CoreData.h>
@interface TGRTimeline : NSObject
@property (strong, nonatomic, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, readonly, getter = isLoading) BOOL loading;
- (id)initWithAccount:(ACAccount *)account;
#import "TGRTimeline.h"
#import "TGRTweet.h"
#import <Social/Social.h>
@interface TGRTimeline ()
@property (nonatomic, readwrite) BOOL loading;
@property (strong, nonatomic) ACAccount *account;
@end
@implementation TGRTimeline
- (id)initWithAccount:(ACAccount *)account
{
self = [super init];
if (self) {
_account = account;
}
return self;
}
- (BOOL)loadNewTweetsWithCompletionHandler:(void (^)(NSError *error))completionHandler
{
if (self.loading) {
return NO;
}
SLRequest *request = [self requestForNewTweets];
[self loadTweetsWithRequest:request completionHandler:completionHandler];
return YES;
- (SLRequest *)requestForNewTweets
{
return nil;
}
- (SLRequest *)requestForOldTweets
{
return nil;
}
- (void)loadTweetsWithRequest:(SLRequest *)request completionHandler:(void (^)(NSError *error))completionHandler
{
self.loading = YES;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error) {
id response = [NSJSONSerialization JSONObjectWithData:responseData
options:0
error:NULL];
TGRTweet *tweet = [NSEntityDescription insertNewObjectForEntityForName:@"TGRTweet"
inManagedObjectContext:self.managedObjectContext];
TGRTweet *tweet = [TGRTweet insertNewObjectInManagedObjectContext:self.managedObjectContext];
TGRTweet *tweet = [TGRTweet importFromDictionary:jsonTweet
inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"TGRTweet"];
[fetchRequest setFetchBatchSize:25];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"identifier"
ascending:NO];
[fetchRequest setSortDescriptors:@[sortDescriptor]];
NSArray *tweets = [self.managedObjectContext executeFetchRequest:fetchRequest error:NULL];