问题描述
|
我将对象练习添加到对象会话中(作为一种关系)。
在视图中,我想获取并显示特定会话对象的练习。
现在,它正在显示数据库中的所有练习,而不仅仅是针对该会话对象。
这两个对象之间的关系称为“练习”。
如果有人可以帮助我,这就是我正在使用的当前代码。
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@\"Exercise\" inManagedobjectContext:self.managedobjectContext];
[fetchRequest setEntity:entity];
LogResultsViewController *logResultsTableViewController = [[LogResultsViewController alloc]init];
[fetchRequest setPredicate:[nspredicate predicateWithFormat: @\"exercises = %@\",logResultsTableViewController.selectedSession]];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@\"name\" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];
更新的代码:
- (void) viewDidLoad
{
NSSet *exercises = [self.selectedSession valueForKey:@\"exercises\"];
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@\"timeStamp\" ascending:YES]];
NSArray *sorted = [exercises sortedArrayUsingDescriptors:sortDescriptors];
sorted = self.exerciseArray;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [exerciseArray count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static Nsstring *CellIdentifier = @\"Cell\";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [exerciseArray objectAtIndex:indexPath.row];
return cell;
}
当我NSLog SelectedSession时,它显示的是:
selectedSession: <Session: 0x7336940> (entity: Session; id: 0x7334f70 <x-coredata://17D44726-23F7-402F-9CBE-2EED96212E14/Session/p1> ; data: {
exercises = \"<relationship fault: 0x5d34680 \'exercises\'>\";
timeStamp = \"2011-05-31 04:41:07 +0000\";
另外,当我NSLog NSSset称为演习时,我得到:
Relationship fault for (<NSRelationshipDescription: 0x711b370>),name exercises,isOptional 1,isTransient 0,entity Session,renamingIdentifier exercises,validation predicates (
),warnings (
),versionHashModifier (null),destination entity Exercise,inverseRelationship exercises,minCount 0,maxCount 0 on 0x7140790
更新:
好的,所以我将cellForRowAtIndex中的代码更改为
Exercise *exercise = (Exercise *)[exerciseArray objectAtIndex:indexPath.row];
cell.textLabel.text = exercise.name;
现在,它显示了练习名称,但它显示了所有会话的相同练习列表,而不仅仅是它所属的会话。
解决方法
如果您的
Session
实例与Exercise
实例有关系,则无需执行另一次访存即可获取会话的练习。您可以按照这种关系直接获取它们-它们将自动加载。从您的代码看来,logResultsTableViewController.selectedSession
是Session
的实例。在这种情况下,您可以按以下方式获得该会话的所有存在(假设这种关系称为“ 9”):
NSSet *exercises = [logResultsTableViewController.selectedSession valueForKey:@\"exercises\"];
然后,您可以根据需要对该NSSet进行排序。