ios - 具有一对多关系的 NSPredicate

标签 ios cocoa-touch core-data nspredicate

我有两个模型之间的一对多关系,user <-->> followers , followers 表由 userId 组成秒。我想发出获取请求以使关联用户访问这些 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];
        }
    }
}

我用来在我的表格 View 中配置单元格的代码:

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

我不确定为什么会这样。

最佳答案

安德斯,

我将尝试提供一些我认为非常适合让您的应用运行的模型的详细信息。

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

enter image description here

一个 User 链接到零个或多个 Follower

让我知道讨论关系。

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

enter image description here

如您所见,rel 是可选,因为用户不能有任何关注者。删除规则是级联,因为如果您删除一个用户,其关注者也将被删除。

user 与用户相反是一对一的关系。看起来像这样。

enter image description here

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

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

现在,如果您针对 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 有零个或多个关注者(Mike、Bob、Todd)。

您使用的提取请求执行以下操作:给我所有用户是 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

我真的建议了解ATTRIBUTESRELATIONSHIPS 之间的区别。

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

关于ios - 具有一对多关系的 NSPredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14967442/

相关文章:

ios - 调试和发布中的不同 URL -iOS

ios - 过渡到具有透明背景图像的 UINavigationBar 会导致黑色闪烁

ios - 设置新的核心数据实体属性值不保存。这与 RestKit 有什么关系吗?

ios - 尽管有主键,但 MagicalRecord 关系映射会复制对象

iOS TabBarController 背景颜色

iOS5 Facebook 整合

ios - UIBezierPath 数组渲染问题(如何使用 CGLayerRef 进行优化)

ios - 获取 NSGenericException 原因为 : '*** Collection <NSConcreteHashTable: 0x282c34140> was mutated while being enumerated.'

ios - cocoapods pod lib 创建和方案

ios - drawRect 没有绘制正确的形状