coredata:加入查询操作

时间:2014-05-14 11:44:30

标签: ios objective-c core-data

enter image description here我必须使用以下查询从两个实体获取数据。 SELECT * FROM Guides AS t1 JOIN Guide_Category_Int AS t2 ON t1.id = t2.guide_id 我知道我们不能使用coredata作为sqlite,因此我在这两个实体之间建立了关系,并使用了以下代码,但无法获得结果。

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
 NSEntityDescription *entity = [NSEntityDescription entityForName:entityname inManagedObjectContext:kAppDelegate.managedObjectContext];
    [fetchRequest setEntity:entity];

 NSString* predicateString = [NSString stringWithFormat:@"id== %@"];//**in that line how should i use predicate for getting result**

 fetchRequest.predicate = [NSPredicate predicateWithFormat:predicateString];

 NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

 [fetchRequest setFetchBatchSize:TOTAL_BATCH_COUNT];

 NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:kAppDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    self.fetchedResultsController = theFetchedResultsController;
    fetchedResultsController_.delegate = self;

    return fetchedResultsController_;

1 个答案:

答案 0 :(得分:0)

Core-Data不支持“JOIN”-s,

它会返回实体,但是我可以看到它有关系,所以你会得到具有属性Guide_Category的指南,

加入后你想要执行什么查询?哪里......?

如果您使用此谓词,您将使用给定实体的ID进行过滤(在我们​​的案例中为Guide-s ID)

NSString* predicateString = [NSString stringWithFormat:@"id== %@"];

你想按类别ID过滤吗?然后使用以下内容,它将仅返回具有该ID

的类别的指南
NSString* predicateString = [NSString stringWithFormat:@"ANY guide.category_id == %@"];