我在两个模型之间有一对多的关系user <-->> followers
,关注者表由userId
s 组成。我想发出 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
------------------------
我不确定为什么会这样。