關係擷取實體我在我的核心數據的數據庫Person
2個實體和Photo
一個Person
與Photo
一個To-Many
關係,Photo
有Person
(Person<-->>Photo)
反向關係。我通過以下方式將我的應用程序的一些初始數據加載到.plist中:與核心數據
-(void)populateDataStorage{
NSString *path = [[NSBundle mainBundle] pathForResource:@"FakeData" ofType:@"plist"];
if(path){
NSArray *plistData = [[NSArray alloc] initWithContentsOfFile:path];
NSEnumerator *enumerator = [plistData objectEnumerator];
NSArray *personResults;
Photo *photo;
Person *person;
id currItem = [enumerator nextObject];
while (currItem != nil) {
photo = (Photo *)[NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext:[flickrFetcher managedObjectContext]];
photo.name = [currItem objectForKey:@"name"];
photo.path = [currItem objectForKey:@"path"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", [currItem objectForKey:@"user"]];
personResults = [flickrFetcher fetchManagedObjectsForEntity:@"Person" withPredicate:predicate];
if ([personResults count] > 0) {
person = [personResults objectAtIndex:0];
}
else {
person = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:[flickrFetcher managedObjectContext]];
person.name = [currItem objectForKey:@"user"];
}
photo.person = person;
[person addPhotosObject:photo];
NSLog(@"Photo %@ added for user %@", photo.name, person.name);
currItem = [enumerator nextObject];
}
[plistData release];
}
}
一切正常加載。然後我使用NSFetchedResultsController
獲取Person
並加載UITableView
,這一切都可行。當用戶點擊該屬於用戶的小區我必須加載另一個UITableView
與選定人員的照片,這樣我就在我的UITableViewController我的照片的表視圖如下:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"person = %@", person];
self.fetchedResultsController = [[FlickrFetcher sharedInstance] fetchedResultsControllerForEntity:@"Photo" withPredicate:predicate];
}
return self;
}
在我viewDidLoad
:
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *error = nil;
[fetchedResultsController performFetch:&error];
for(Photo *photo in person.photos){
NSLog(@"Photo %@ belongs to %@", photo.name, person.name);
}
NSLog(@"Photo count %d", [[fetchedResultsController fetchedObjects] count]);
}
計數是給我的0,我從來沒有這個人的照片,但我知道他們是正確保存,因爲它們都打印出在Person
傳遞。任何想法我可能做錯了什麼?
在我FlickrFetcher
類獲得NSFetchedResultsController
編輯
實用方法:
- (NSFetchedResultsController *)fetchedResultsControllerForEntity:(NSString*)entityName withPredicate:(NSPredicate*)predicate {
NSFetchedResultsController *fetchedResultsController;
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Add a predicate if we're filtering by user name
if (predicate) {
[fetchRequest setPredicate:predicate];
}
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:@"Root"];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
return [fetchedResultsController autorelease];
}
我在我UITableViewController
創建自己的NSFetchedResultsController
:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Photo" inManagedObjectContext:[flickFetcher managedObjectContext]];
[request setEntity:entity];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:descriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"person = %@", person];
[request setPredicate:predicate];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[flickFetcher managedObjectContext] sectionNameKeyPath:nil cacheName:@"Root"];
[request release];
[descriptor release];
[sortDescriptors release];
NSError *error = nil;
[fetchedResultsController autorelease];
[fetchedResultsController performFetch:&error];
我看不出區別。
感謝您的提示,我設法從上下文中檢索照片,這很奇怪,因爲我所做的只是創建自己的提取結果控制器,這與我之前使用的實用程序方法沒有什麼不同,但現在表格單元格不再是' t設置,cellForRowAtIndex永遠不會因爲某種原因被調用,我將更新代碼以顯示兩個fetchedResultsControllers。 – 8vius
沒有cellForRowAtIndex:=沒有tableView.dataSource,或numberOfRowsInSection = 0 – bshirley
是numberOfRowsInSection交易,這沒有把戲。注意我的fetchedResultsControllers有什麼不同?除了升序參數 – 8vius