0

我在两个模型之间有一对多的关系user <-->> followers,关注者表由userIds 组成。我想发出 fetch 请求以将关联的用户带到 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 
------------------------

我不确定为什么会这样。

4

2 回答 2

3

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

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

这将返回所有Followersself.user. 希望这可以帮助。

于 2013-02-19T21:18:03.300 回答
3

安德斯,

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

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

在此处输入图像描述

AnUser链接到零个或多个Followers。

让我知道讨论关系。

followersFollower是与实体的一对多关系。它是这样设置的。

在此处输入图像描述

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

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

在此处输入图像描述

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

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

现在,如果您创建一个 fetch 请求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并使其成为 type NSString。正如你对User实体所做的那样(我想)。

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

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

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

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

于 2013-02-23T11:59:53.200 回答