1

我有一个存储高尔夫球场信息的应用程序。我将数据保存到三个实体:回合、课程和发球台。我以这种方式构建架构,因为 Rounds 和 Courses 之间存在二多关系,并且 Course 可以有多个 Tee(蓝色、白色、金色等)

我有一个 UITableview,我想在其中显示每一轮的结果。但是,我想显示来自 Rounds 实体和 Courses 实体的数据。理想情况下,我也想展示该回合的 Tee,但这不是优先事项。

我的问题是,如何使用 FetchResultsController 从三个实体中获取数据并将其显示在 UITableview 的单个单元格中?

这是我将数据保存到实体的方式:

HandicapAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext* context = [appDelegate managedObjectContext];

    NSEntityDescription *roundsEntity = [NSEntityDescription entityForName:@"Rounds" inManagedObjectContext:context];
NSFetchRequest *request =[[NSFetchRequest alloc]init];
[request setEntity:roundsEntity];

Rounds * rounds = [NSEntityDescription insertNewObjectForEntityForName:@"Rounds" inManagedObjectContext:context];
[rounds setValue:score forKey:@"roundScore"];
[rounds setValue:date   forKey:@"roundDate"];
[rounds setValue:differential   forKey:@"roundDifferential"];

Courses * courses = [NSEntityDescription insertNewObjectForEntityForName:@"Courses" inManagedObjectContext:context];
[courses setValue:rating forKey:@"courseRating"];
[courses setValue:slope forKey:@"courseSlope"];
[courses setValue:courseName forKey:@"courseName"];
rounds.courses = courses;

Tee * tee = [NSEntityDescription insertNewObjectForEntityForName:@"Tee" inManagedObjectContext:context];
[tee setValue:teeColor forKey:@"teeColor"];
courses.tees = tee;

NSError *error;
[context save:&error];

然后这是我的 FetchedResultsController

 if (_fetchedResultsController != nil)
{
    return _fetchedResultsController;
}

// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

fetchRequest.entity = [NSEntityDescription entityForName:@"Rounds" inManagedObjectContext:self.managedObjectContext];

// Set the batch size 
[fetchRequest setFetchBatchSize:20];

// Set the sort descriptor
NSSortDescriptor *  date = [[NSSortDescriptor alloc] initWithKey: @"roundDate"
                                                        ascending: NO];
NSArray *           sortDescriptors = [NSArray arrayWithObjects: date, nil];
fetchRequest.sortDescriptors = sortDescriptors;



// Initialize fetched results controller - creates cache
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
                                                         initWithFetchRequest: fetchRequest
                                                         managedObjectContext: self.managedObjectContext
                                                         sectionNameKeyPath: nil
                                                         cacheName: @"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

// handle errors
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
    /*
     Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return _fetchedResultsController;

如何在 tableview 单元格中获取该回合的课程名称和 tee(这将是 tee 颜色)?

谢谢!!

4

1 回答 1

1

只要遵循关系。如果轮次和课程之间存在一对多关系,则需要在课程上rounds建立关系,在轮次上建立反向关系,例如course. 因此,如果您为实体生成了子类,您可以像这样访问课程名称:

NSString *courseName = round.course.name;
于 2013-09-02T15:06:10.377 回答