Skip to content

Instantly share code, notes, and snippets.

@quique123
Created March 5, 2010 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quique123/322994 to your computer and use it in GitHub Desktop.
Save quique123/322994 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"
@implementation PeopleListViewController
@synthesize filePath, dataForTableView, photosRC;
-(void)viewDidLoad {
[super viewDidLoad];
[self loadCoreData];
//[self getCoreData];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
-(void)loadCoreData{
NSLog(@"Entering loadCoreData");
// call singleton and make context
FlickrFetcher *myConnection=[FlickrFetcher sharedInstance];
//Check if db exists, otherwise call plist file and populate
if ([myConnection databaseExists]==YES){
NSLog(@"db EXISTS! Going to getCoreData...");
[self getCoreData];}
else {
NSLog(@"db doesnt exit, CREATE IT!");
NSManagedObjectContext *managedObjectContext=[myConnection managedObjectContext];
NSLog(@"file context created!");
// GET PLIST FILE INFO
filePath = [[NSBundle mainBundle] pathForResource:@"FakeData" ofType:@"plist"];
if (filePath){
NSLog(@"file path gotten!");
// CREATE FLICKR ARRAY FROM PLIST FILE PATH
NSArray *twitterIds = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"array created!");
for (NSDictionary *dict in twitterIds){
// Step 1: Create PhotoObject
NSLog(@"creating newPhoto object!");
Photo *newPhoto = (Photo*)[NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext:managedObjectContext];
// Step 2: Set PhotoProperties
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"]];
}// for dictionary loop, create object and set properties
//Step 3: Save to psc after the loop puts everything into the context
//After i figure how to fetch ill create the person objects
NSError *error;
if(![managedObjectContext save:&error]){
NSLog(@"Error:%@",error);}// if my context saves with error, log the error
for (NSDictionary *dict in twitterIds){
// Step 4: Create PersonObject
NSLog(@"creating newPerson object!");
Person *newPerson = (Person*)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:managedObjectContext];
// Step 5: Set PersonProperties
NSLog(@"Entering photoName:%@",[dict objectForKey:@"name"]);
[newPerson setPhotoName:[dict objectForKey:@"name"]];
NSLog(@"SettingOwnerName:%@",[dict objectForKey:@"user"]);
[newPerson setUserName:[dict objectForKey:@"user"]];
}
//Step 6: Save to psc after the loop puts everything into the context
//After i figure how to fetch ill create the person objects
//NSError *error;
if(![managedObjectContext save:&error]){
NSLog(@"Error:%@",error);}// if my context saves with error, log the error
}//if filepath exists
}//else if db doesnt exist
}
-(void)getCoreData{
NSLog(@"Entering getCoreData");
FlickrFetcher *myConnection=[FlickrFetcher sharedInstance];
/*
//New fetchrequest using [FlickrFetcher fetchManagedObjectsForEntity:withPredicate] ARRAY RETURNED//
//THIS ONE WORKS FINE :):):):):):):):):):)
self.dataForTableView=[myConnection fetchManagedObjectsForEntity:@"Photo" withPredicate:nil];
Photo *currentPhoto = [self.dataForTableView objectAtIndex:0];
NSLog(@"Photo name: %@", currentPhoto.photoName);
*/
//New fetchrequest using [FlickrFetcher fetchedResultsControllerForEntity:withPredicate:];
photosRC = [myConnection fetchedResultsControllerForEntity:@"Photo" withPredicate:nil];
NSError *error;
NSLog(@"%@", error);
BOOL success = [photosRC performFetch:&error];
if(success = YES) {NSLog(@"PERFETO");} else { NSLog(@"ERRRRRRRRRRROOOOOoooooooooorrrrrrrrrr");}
NSLog(@"Leaving getCOreData");
}
#pragma mark -----------------------------------------------------------------------
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"NOSITV");
return [[photosRC sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[photosRC sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"here at CFRAIP Identifier");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
Photo *newPhoto = [photosRC objectAtIndexPath:indexPath];
cell.textLabel.text = newPhoto.photoName;
// Configure the cell with data from the managed object.
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"DSRAIP");
// Navigation logic may go here. Create and push another view controller.
//PersonDetailViewController *detailView = [[PersonDetailViewController alloc] initWithStyle:(UITableViewStyle)UITableViewStyleGrouped];
//detailView.someTwit = [persons objectAtIndex:indexPath.row];
//[self.navigationController pushViewController:detailView animated:YES];
//[detailView release];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment