繁体   English   中英

NSPredicate具有多对多关系

[英]NSPredicate with to-many relationship

我在两个模型之间建立了一对多关系, user <-->> followers ,followers表由userId组成。 我想发出一个获取请求,以将关联的用户添加到theese userId 我的谓词当前看起来像这样:

predicate = [NSPredicate predicateWithFormat:@"ANY userId IN %@", self.user.followers];

结果为0。 我想知道这个谓词应该是什么样子?

更新资料

我用于将跟随者对象添加到用户的对象的代码(来自JSON数据):

+ (void)insertFollowersFromDict:(NSDictionary *)dictionary forUser:(User *)user  inManagedObjectContext:(NSManagedObjectContext*)moc
{
    NSArray *followers = [dictionary objectForKey:@"data"];

    if (followers.count != 0) {
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Follower" inManagedObjectContext:moc];
        for (NSString *followerId in followers) {
            Follower *newFollower = [[Follower alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:moc];
            NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
            newFollower.user = [User findUser:[numberFormatter  numberFromString:followingId] inManagedObjectContext:moc];
            [user addFollowersObject:newFollower];
        }
    }
}

我用于在表格视图中配置单元格的代码:

// This for some reason always result in the same user. 
Follower *follower = [self.fetchedResultsController objectAtIndexPath:indexPath];
User *cellUser = follower.user;

// Always self.user
cell.text = cellUser.name

------------------------
I Anders               I
------------------------
------------------------
I Anders               I 
------------------------

我不确定为什么会这样。

您应该在对象之间建立关系。 您不必将userID存储在Follower对象中。 这样,您可以简单地编写:

predicate = [NSPredicate predicateWithFormat:@"user = %@", self.user];

这将返回与self.user有关系的所有Followers 希望这可以帮助。

安德斯

我将尝试提供一些有关该模型的详细信息,我认为该模型非常适合让您的应用正常工作。

首先,您的模型应如下所示(如您所见,我跳过了属性)。

在此处输入图片说明

User链接到零个或多个Follower

让我知道有关关系的讨论。

followers是与Follower实体的一对多关系。 这样设置。

在此处输入图片说明

如您所见,由于用户不能具有任何关注者,因此rel是可选的。 删除规则是级联的,因为如果删除用户,则其关注者也会被删除。

相反, user是一个用户。 看起来像这样。

在此处输入图片说明

它不是可选的,因为关注者只能与用户一起存在。 这是无效的,因为如果您删除关注者,则更改将不会对关联的用户生效。

注意我根据您提供的规格创建了模型,但我认为也可以对其进行扩展以适应多对多关系。 这意味着:不同的用户可以具有相同的关注者。

现在,如果您针对Follower创建访存请求,则可以通过以下方式检索属于特定用户的所有Follower

NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"Follower"]
[request setPredicate:[NSPredicate predicateWithFormat:@"user == %@", self.user]];
NSArray* results = // execute the request here

// results will contain the followers for a specific user (i.e. the one you used in the predicate)

希望能有所帮助。

编辑

NSManagedObject* follower = [followerResults objectAtIndex:[indexPath row]]; // this gives you a follower, put some error control if followerResults has no elements
NSManagedObject* user = [follower objectForKey:@"user"]; // this gives you the user associated to that follower

编辑2

我不清楚您的意思,但由于您使用的是正确的行为

cell.text = cellUser.name

通过这种方式,您可以检索与用户关联的名称。 只有一个用户与该组关注者相关联

例如,用户安德鲁有零个或多个关注者(迈克,鲍勃,托德)。

您使用的提取请求将执行以下操作: 给我所有用户为Andrew的关注者。

如果您想显示关注者的变化,例如

cell.text = follower.name; // if Follower entity has a name attribute

非常非常重要

向您的Follower实体添加一个新属性(没有关系)。 称它为name并使其类型为NSString 就像您对User实体所做的那样(我想)。

然后,当您创建新的Follower对象时,将其设置为

 newFollower.user = // set the user
 newFollower.name = // set the name

我真的建议理解属性关系之间的区别。

前者是特定实体的属性(例如,姓名,年龄,性别)。 后者是实体之间的链接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM