xcode 获取一对多关系数据的核心数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16265572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Core data fetching one to many relationship data
提问by Jonathan Thurft
I've searched around but I am still unclear about various concepts that I hope someone clarifies for me.
我四处搜索,但我仍然不清楚各种概念,希望有人为我澄清。
My confusion and headache comes when trying to save into the Exercise / Routine entities while respecting some sort of user's ID.
当我试图在尊重某种用户 ID 的同时保存到练习/例程实体中时,我会感到困惑和头痛。
For my understanding and reading around core data the way I setted core data up, it should work and be able to pull and save information into Routine and Exercise while respecting the user they are working with. Could anyone correct me if i am wrong?
就我设置核心数据的方式对核心数据的理解和阅读而言,它应该可以工作,并且能够在尊重与他们合作的用户的同时将信息提取并保存到 Routine 和 Exercise 中。如果我错了,有人可以纠正我吗?
I think I have to do a query that looks something like (in SQL terms)
我想我必须做一个看起来像(用 SQL 术语)的查询
INSERT INTO Routine (Values) WHERE Routine.userID = user.usersExercise
Is that correct? if that is correct. Now the problem that I've is that I have no idea how to make an inner join of these two entities in coreData code in order to produce a query like that.
那是对的吗?如果那是正确的。现在我遇到的问题是我不知道如何在 coreData 代码中对这两个实体进行内部连接以生成这样的查询。
Any help is really appreciate it!
任何帮助真的很感激!
采纳答案by Dev2rights
Yes you need to use NSPredicate to get the data related to a specific user. If you are populating a UICollection view then i would recommend also reading up on NSFetchedResultsController.
是的,您需要使用 NSPredicate 来获取与特定用户相关的数据。如果您正在填充 UICollection 视图,那么我建议您还阅读 NSFetchedResultsController。
NSFetchRequest *fetchRequestItems = [[NSFetchRequest alloc] init];
NSEntityDescription *entityItem = [NSEntityDescription entityForName:@"Routine" inManagedObjectContext:self.managedObjectContext];
[fetchRequestItems setEntity:entityItem];
User* myUser = //Code for getting current user out of data store based on some paramator
[fetchRequestItems setPredicate:[NSPredicate predicateWithFormat:@"userID == %@",myUser]];
//Sort by last edit ordered
NSArray *sortDescriptors = [NSArray arrayWithObjects:nil];
[fetchRequestItems setSortDescriptors:sortDescriptors];
NSArray* Routines = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
The thing to note here is you need to set up inverse relationships on your Routine / Exercise objects to compare against the current user in the predicate. Which you have for userID which i presume is an inverse relationship ?
这里要注意的是,您需要在 Routine / Exercise 对象上设置反向关系,以与谓词中的当前用户进行比较。你有哪些我认为是反向关系的用户 ID?
So from the comment i take you havent set the inverse relationship so here is a rough example.
因此,根据我的评论,您还没有设置反向关系,因此这是一个粗略的示例。
When making a new User on your store you would get somethign like this:
在您的商店中创建新用户时,您会得到如下信息:
User *thisUser = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.managedObjectContext];
thisUser.eMail = emailAddress.text;
//And any other paras you might have
Then when making a routine for that user you would do this:
然后在为该用户制定例程时,您将执行以下操作:
Routine *newRoutine = [NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];
newRoutine.userId = thisUser;
Then make sure to save the managed object to write the changes to the data store. This then links the 2 objects on the routine side, dont forget to link it on the Users side too for a strict inverse relationship.
然后确保保存托管对象以将更改写入数据存储。然后在例程端链接 2 个对象,不要忘记在用户端也链接它以获得严格的反向关系。
To get the user its exactly like i mentioned above:
为了让用户完全像我上面提到的那样:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError* error;
NSArray* Users = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
If you have 1 user then its the first element in the array, if you have many users then use predicates to find the one based on your FB id.
如果您有 1 个用户,则它是数组中的第一个元素,如果您有很多用户,则使用谓词根据您的 FB id 找到那个。