【发布时间】:2015-06-17 23:24:24
【问题描述】:
我有一个 CoreData 实体,它在不同长度的比赛中具有多个性能记录。我想在每个比赛中为每个运动员获得最佳表现,然后将结果放在CoreData Table View中。
这是我为实现此目的而想出的技巧。但是,这会干扰我将结果放入 TableView 中,其中比赛距离是部分标题:
- (void)setupFetchedResultsController // attaches an NSFetchRequest to this UITableViewController
{
MarksFromMeets* results = nil;
NSMutableArray * bestMarks = [[NSMutableArray alloc] init];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MarksFromMeets"];
// NSLog(@"self.athlete.athleteID = %@",self.athlete.athleteID);
request.predicate = [NSPredicate predicateWithFormat:@"(ANY whoRan.athleteID in %@) AND (eventPR > 0)",
self.athleteIDsForPredicate];
/* Call the records for selected athletes (PLURAL) */
request.sortDescriptors = [NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:@"whoRan.athleteID" ascending:NO],
[NSSortDescriptor sortDescriptorWithKey:@"event" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"sortMark" ascending:YES],
nil];
以下代码块中的代码是我为每个运动员提取最佳表现的地方。
NSArray * allMarks = [self.headToHeadManagedObjectContext executeFetchRequest:request error:nil];
MarksFromMeets* tempMark;
for (MarksFromMeets* athletePRs in allMarks) {
if (tempMark == nil) {
tempMark = athletePRs;
} else {
if ([athletePRs.whoRan.athleteID isEqualToString:tempMark.whoRan.athleteID]) {
if ([athletePRs.event isEqualToString:tempMark.event]) {
if (athletePRs.sortMark < tempMark.sortMark) {
tempMark = athletePRs;
}
} else {
[bestMarks addObject:tempMark];
tempMark = athletePRs;
}
} else {
[bestMarks addObject:tempMark];
tempMark = athletePRs;
}
}
}
if (debug == 1) NSLog(@"bestMarks count: %lu \nallMarks count: %lu", (unsigned long)[bestMarks count], [allMarks count]);
[self setResultsArray:bestMarks];
NSArray bestMarks 有 27 个 NSManagedObjects,而 fetch 请求返回 35 个。
下面的代码是我过去并且仍然希望填充我的 CoreData TableView 的方式。
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.headToHeadManagedObjectContext
sectionNameKeyPath:@"event"
cacheName:nil];
NSError* error = nil;
results = [[self.headToHeadManagedObjectContext executeFetchRequest: request error:&error]objectAtIndex:0];
NSError* requestError = nil;
NSUInteger myCount = [self.headToHeadManagedObjectContext countForFetchRequest:request error:&requestError];
NSLog(@"In %@ and count of results = %lu", NSStringFromClass([self class]),(unsigned long)myCount);
if (!myCount || myCount == 0) {
NSMutableString* titleText = [NSMutableString stringWithFormat:@"No Records Found"];
NSMutableString* messageText = [NSMutableString stringWithFormat:@"There were no records found for the %lu athletes selected. Contact ITrackPerfomance using this list of Athlete ID's: %@", (unsigned long)[self.athleteIDsForPredicate count], self.athleteIDsForPredicate];
NSString* cancelText = @"Okay";
[self displayAlertBoxWithTitle:(NSString*)titleText message:(NSString*) messageText cancelButton:(NSString*) cancelText];
}
}
有没有办法用 NSFetchRequest 谓词做到这一点?
或者我可以通过将托管对象放入 NSDictionaries 的 NSArray 中来创建部分,其中每个 NSDictionary 是不同的比赛距离?
感谢您的帮助。如果我完全偏离了正确的道路并且有更有效的方法来实现这一点,我也会很高兴知道这一点!
【问题讨论】:
标签: ios uitableview core-data nspredicate