2012-01-24 113 views
0

我有一个数据模型,看起来像这样:核心数据获取请求

enter image description here

我告诉顾客在tableview中,当用户选择一个客户列表,它则显示房间列表该客户 - 这一切目前正常工作。

我得到的问题是当我尝试显示房间的详细视图。当用户选择房间时,它将显示包含该房间所有值的视图。 (以及应该做的)。我有这个工作在一定程度上,但如果我有两个房间命名相同即“ - 卧室1”,为两个不同的客户,然后它不显示正确的房间数据。

这里是我使用的代码:

AppDelegate_Shared *appDelegate = [[UIApplication sharedApplication] delegate]; 

    NSManagedObjectContext *context = [appDelegate managedObjectContext]; 

    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Rooms" inManagedObjectContext:context]; 

    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 

    [request setEntity:entityDesc]; 

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(Room = %@)", titleStr]; 

    [request setPredicate:pred]; 

    NSManagedObject *matches = nil; 
    NSError *error; 

    NSArray *objects = [context executeFetchRequest:request error:&error]; 

    if ([objects count] == 0) 
    { 
     NSLog(@"No matches"); 
    } else 
    { 
     matches = [objects objectAtIndex:0]; 
     NSLog(@"matches found"); 
     aLbl.text = [matches valueForKey:@"DimA"]; 
    } 
    [request release]; 
    [NSFetchedResultsController deleteCacheWithName:nil]; 
    [self.fetchedResultsController.fetchRequest setPredicate:pred]; 

我想发生什么事,是详细信息视图显示了基于什么客户已经选择的细节。

任何帮助深表感谢......

感谢

回答

1

更改您的谓语也包括您正在搜索的客户。

想象一下这是SQL(假设你已经使用了SQL),谓词就是你的WHERE子句。如果房间不是唯一的,那么这将返回具有相同名称的所有房间。

你想要的是ROOM == roomName和customer == customerId的房间。

+0

[NSPredicate predicateWithFormat:@“(Room ==%@)AND(CustomerRelationship ==%@)”,titleStr,customerManagedObject] – zrxq

+0

谢谢,就是这么做的!谢幕 :-) –

相关问题