从CoreData获取记录?

时间:2010-03-28 22:36:22

标签: iphone uitableview core-data

我正在尝试从.plist向CoreData添加值,并从coreData获取并在UITable上显示它们。我从.plist中获取并将它们发送到CoreData。但我无法从CoreData entity获取它们并设置为某些NSMutableArray ..这里是我的代码

修改:已解决

  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]];


} 

这是我的tableView功能

- (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;
}

1 个答案:

答案 0 :(得分:0)

这里是我的解决方案。我创建了一个NSMutable数组,并使用setPeopleList从fetchedResultsController中添加值fetchedObjects.Also使用set操作我在头文件中创建了我的数组并添加了

@property (nonatomic, retain) NSMutableArray *peopleList; 

并在我创建的.m文件中

   @synthesize peopleList;

我用过

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