getting record from CoreData ?

Posted by Meko on Stack Overflow See other posts from Stack Overflow or by Meko
Published on 2010-03-28T22:36:22Z Indexed on 2010/03/29 20:13 UTC
Read the original article Hit count: 477

Filed under:
|
|

Hi. I am trying to add value to CoreData from .plist and get from coreData and show them on UITable. I made take from .plist and send them to CoreData .But I cant take them from CoreData entity and set to some NSMutable array.. Here my codes

Edit : SOLVED

NSString *path = [[NSBundle mainBundle] pathForResource:@"FakeData" ofType:@"plist"]; NSArray *myArray=[[NSArray alloc] initWithContentsOfFile:path];

FlickrFetcher *flickrfetcher =[FlickrFetcher sharedInstance];

managedObjectContext =[flickrfetcher managedObjectContext];

for(NSDictionary *dict in myArray){

    Photo *newPhoto = (Photo *)[NSEntityDescription
                                insertNewObjectForEntityForName:@"Photo"
                                inManagedObjectContext:managedObjectContext];


    // set the attributes for the photo
    NSLog(@"Creating Photo: %@", [dict objectForKey:@"name"]);
    [newPhoto setName:[dict objectForKey:@"name"]];
    [newPhoto setPath:[dict objectForKey:@"path"]];

    NSLog(@"Person is: %@", [dict objectForKey:@"user"]);
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name ==%@",[dict objectForKey:@"user"]];
    NSMutableArray  *peopleArray =  (NSMutableArray *)[flickrfetcher
                                                       fetchManagedObjectsForEntity:@"Person" withPredicate:predicate];



    NSEnumerator *enumerator = [peopleArray objectEnumerator];
    Person *person;
    BOOL exists = FALSE;

    while (person = [enumerator nextObject]) {
        NSLog(@" Person is: %@ ", person.name);
        if([person.name  isEqualToString:[dict objectForKey:@"user"]]) {
            exists = TRUE;
            NSLog(@"-- Person Exists : %@--", person.name);
            [newPhoto setPerson:person];
        }
    }




    // if person does not already exist then add the person
    if (!exists) {
        Person *newPerson = (Person *)[NSEntityDescription
                                       insertNewObjectForEntityForName:@"Person"
                                       inManagedObjectContext:managedObjectContext];

        // create the person
        [newPerson setName:[dict objectForKey:@"user"]];
        NSLog(@"-- Person Created : %@--", newPerson.name);

        // associate the person with the photo
        [newPhoto setPerson:newPerson];

    }

//THis IS myArray that I want to save value in it and them show them in UITABLE
personList=[[NSMutableArray alloc]init];






    NSLog(@"lllll  %@",[personList objectAtIndex:0]);
    // save the data
    NSError *error;
    if (![managedObjectContext save:&error]) {
        // Handle the error.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);

    }
}

//To access core data objects you can try the following:


// setup our fetchedResultsController
fetchedResultsController = [flickrfetcher fetchedResultsControllerForEntity:@"Person" withPredicate:nil];

// execute the fetch
NSError *error;
BOOL success = [fetchedResultsController performFetch:&error];
if (!success) {
    // Handle the error.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);
}

// save our data //HERE PROBLEM THAT I DONW KNOW WHERE IT IS SAVING
//[personList addObject:(NSMutableArray *)[fetchedResultsController fetchedObjects]];

[self setPersonList:(NSMutableArray *)[fetchedResultsController
                                    fetchedObjects]];


} 

Here my tableview function

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"ss %d",[personList count]);

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[personList objectAtIndex:indexPath.row];



// Set up the cell...

return cell;
}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about core-data