如果获取或关系是问题,则无法判断

时间:2011-05-30 20:57:18

标签: iphone objective-c core-data

我正在将对象练习添加到对象会话(作为关系)。

在视图中,我想获取并显示特定会话对象的练习。

现在它显示的是数据库中的所有练习,而不仅仅是该会话对象。

两个对象之间的关系称为“练习”。

如果有人可以帮助我,这是我用于获取的当前代码。

    // 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: <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;

现在它显示了练习名称,但它显示的是所有会话的相同练习列表,而不仅仅是它所属的会话。

1 个答案:

答案 0 :(得分:0)

如果Session的实例与Exercise的实例有关系,则无需再执行其他操作即可获得会话练习。您可以按照关系直接获取它们 - 它们将自动加载。从您的代码看,logResultsTableViewController.selectedSessionSession的实例。如果是这种情况,您可以按如下方式获得该会话的所有练习(假设关系称为exercises):

NSSet *exercises = [logResultsTableViewController.selectedSession valueForKey:@"exercises"];

然后,您可以根据需要对NSSet进行排序。