Skip to content

Instantly share code, notes, and snippets.

@quique123
Created February 26, 2010 14:52
Show Gist options
  • Save quique123/315773 to your computer and use it in GitHub Desktop.
Save quique123/315773 to your computer and use it in GitHub Desktop.
//
// PeopleListViewController.m
// Paparazzi
//
// Created by Marcio Valenzuela on 2/20/10.
// Copyright 2010 Personal. All rights reserved.
//
#import "PeopleListViewController.h"
#import "Photo.h"
#import <CoreData/CoreData.h>
@implementation PeopleListViewController
@synthesize persons, filePath, managedObjectContext;
-(id)initWithStyle:(UITableViewStyle)style{
NSLog(@"INITWITHSTYLE");
//self = [super initWithStyle:style];
if (self == nil)
return nil;
self.title = @"People List";
//Commented out because iniwithstyle doesnt get called unless its a UITableViewController
//operationQueue = [[NSOperationQueue alloc]init];
//[operationQueue setMaxConcurrentOperationCount:1];
//NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
//self.cachedImages = dict;
//[dict release];
return self;
}
-(void)viewDidLoad {
NSLog(@"VIEWDIDLOAD");
[super viewDidLoad];
operationQueue = [[NSOperationQueue alloc]init];
[operationQueue setMaxConcurrentOperationCount:1];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//[self showLoadingIndicators];
[self beginLoadingFlickrData];
}
- (void)didReceiveMemoryWarning {
NSLog(@"DIDRECEIVEMEMORYWARNING");
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
//[cachedImages removeAllObjects];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
NSLog(@"VIEWDIDUNLOAD");
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
NSLog(@"DEALLOC");
[persons release];
[operationQueue release];
[super dealloc];
}
#pragma mark -----------------------------------------------------------------------
#pragma mark Loading Progress UI
/*
- (void)showLoadingIndicators{
NSLog(@"SHOWLOADINGINDICATORS");
if (!spinner) {
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
loadingLabel = [[UILabel alloc] initWithFrame:CGRectZero];
loadingLabel.font = [UIFont systemFontOfSize:20];
loadingLabel.textColor = [UIColor grayColor];
loadingLabel.text = @"Loading...";
[loadingLabel sizeToFit];
static CGFloat bufferWidth = 8.0;
CGFloat totalWidth = spinner.frame.size.width + bufferWidth + loadingLabel.frame.size.width;
CGRect spinnerFrame = spinner.frame;
spinnerFrame.origin.x = (self.tableView.bounds.size.width - totalWidth) / 2.0;
spinnerFrame.origin.y = (self.tableView.bounds.size.height - spinnerFrame.size.height) / 2.0;
spinner.frame = spinnerFrame;
[self.tableView addSubview:spinner];
CGRect labelFrame = loadingLabel.frame;
labelFrame.origin.x = (self.tableView.bounds.size.width - totalWidth) / 2.0 + spinnerFrame.size.width + bufferWidth;
labelFrame.origin.y = (self.tableView.bounds.size.height - labelFrame.size.height) / 2.0;
loadingLabel.frame = labelFrame;
[self.tableView addSubview:loadingLabel];
}
}
- (void)hideLoadingIndicators{
NSLog(@"HIDELOADINGINDICATORS");
if (spinner) {
[spinner stopAnimating];
[spinner removeFromSuperview];
[spinner release];
spinner = nil;
[loadingLabel removeFromSuperview];
[loadingLabel release];
loadingLabel = nil;
}
}
*/
#pragma mark -----------------------------------------------------------------------
#pragma mark Twitter Data Loading OPERATIONQUEUE #1 : LOAD FLICKR DATA
-(void)beginLoadingFlickrData{
NSLog(@"BEGINLOADINGflickrDATA NAMES QUEUE");
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(synchronousLoadFlickrData) object:nil];
[operationQueue addOperation:operation];
[operation release];
}
-(void)synchronousLoadFlickrData{
NSLog(@"BEGINLOADINGflickr dictionary");
// Beause this is ran inside a seprate thread we have to be real careful
// that the data we play with here doesn't clobber another thread that is
// running at the exact same time. We will build a temprary array that we
// will assign the instance variable to only on the main thread.
//Check if db exists, otherwise call plist file and populate
//FlickrFetcher *sharedInstance = [FlickrFetcher sharedInstance];
PaparazziAppDelegate *appDelegate= (PaparazziAppDelegate *)[[UIApplication sharedApplication] delegate];
//NSLog(@"%@", [sharedInstance databaseExists]);
//NSLog(@"%@", [FlickrFetcher databaseExists]);
if(1==1)
{
// GET PLIST FILE INFO
filePath = [[NSBundle mainBundle] pathForResource:@"FakeData" ofType:@"plist"];
if (filePath)
{
// CREATE FLICKR ARRAY FROM PLIST FILE PATH
NSArray *twitterIds = [NSArray arrayWithContentsOfFile:filePath];
for (NSDictionary *dict in twitterIds)
{
// Step 1: Create Object
Photo *newPhoto = (Photo*)[NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext:managedObjectContext];
// Step 2: Set Properties
NSLog(@"Entering Photo Name:%@",[dict objectForKey:@"name"]);
[newPhoto setPhotoName:[dict objectForKey:@"name"]];
NSLog(@"SettingPhotoURL:%@",[dict objectForKey:@"path"]);
[newPhoto setPhotoURL:[dict objectForKey:@"path"]];
NSLog(@"SettingOwnerName:%@",[dict objectForKey:@"user"]);
[newPhoto setOwnerName:[dict objectForKey:@"user"]];
}
// Step 3: Save Object
[appDelegate saveAction];
//NSLog(@"dict object count:%i", [dict count]);
} else { NSLog(@"no filepath");}
}else { NSLog(@"1 does equal 1");}
//NSArray *tempArray = [[NSArray alloc] initWithObjects:@"one",nil];
[self performSelectorOnMainThread:@selector(didFinishLoadingFlickrDataWithResults:) withObject:nil waitUntilDone:NO];
NSLog(@"leaving synchronousLoadFlickrData");
}
-(void)didFinishLoadingFlickrDataWithResults:(id)result{
NSLog(@"DIDFINISHLOADINGTWITTERDATAWITHRESULTS ARRAY");
//Get Core Data from Flickr CD db
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Photo"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *items = [self.managedObjectContext
executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
NSLog(@"%@", items);
//[self hideLoadingIndicators];
//[self.tableView reloadData];
//[self.tableView flashScrollIndicators]; // I guess this is only if you have more users that the screenful which isnt my case
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment