【发布时间】:2016-04-05 23:14:49
【问题描述】:
我在 mysql 和 php 中有一个跟踪系统,它只使用一个表,如下所示:
关注
id|userid|followerid
(还有一个users表:id|username|pass)
每当用户关注另一个用户时,都会在关注列中使用 followerid 并在 userid 列中使用被关注的人进行条目。
我现在正尝试使用 Web 服务从 iOS 应用程序访问它。
对于 VC,我希望能够列出用户,但通过链接 NSPredicate 在用户的关注者和用户关注的人之间切换。这对于使用 php/sql 来说是微不足道的,但在转换为 CoreData 时需要花费一些时间。
现在我在核心数据中有用户实体,具有以下属性:
id|用户名|密码
并复制 mysql 架构,我还有一个以下实体 fid|fuserid|flowerid
反过来,我为用户与关注者创建了一对多关系,并为用户与关注者创建了反向一对多关系,因为给定用户(例如应用程序的用户)可以有很多关注者,也可以关注很多人。
Web 服务当前设置为向用户和每个用户提供一组关注者 ID,如下所示 编号:1 名称:鲍勃 追随者:2,3,4
此时我被难住了。我可以填充用户实体。我还可以用所有关注用户的人和用户关注的人填充关注者实体。
但是,我不知道要向 Core data 传达哪些关注者与哪个用户相关,而且我也不知道如何编写谓词,例如,仅拉取应用程序用户的关注者。
希望有任何建议。
现在我的 NSManagedObjects 如下所示:
//User.h
#import "Following.h"
@class Following;
@interface User : NSManagedObject
//in user entity
@property (nonatomic, retain) NSNumber * uid;
@property (nonatomic, retain) NSString * uname;
@property (nonatomic, retain) NSString *upass;
//this is relationship
@property (nonatomic, retain) Following *following;
@end
//and Following.h
#import "User.h"
@class User;
@interface Following : NSManagedObject
@property (nonatomic, retain) NSNumber * fid;
@property (nonatomic, retain) NSNumber * fuserid;
@property (nonatomic, retain) NSNumber * followerid;
//this is relationship
@property (nonatomic) User *user;
@end
从网络服务导入和保存用户后,我尝试在关注实体中保存一些关于关注者的信息,但不确定我在这里做什么......
NSSet*followerids = importUser.followerids;
NSInteger *folnum =[followerids count];
if (folnum>0) {
for (id item in followerids) {
Following *newFollowing = [NSEntityDescription insertNewObjectForEntityForName:@"Following" inManagedObjectContext:self.managedObjectContext];
newFollowing.fuserid = useridnum;
NSNumber *itemnum = [NSNumber numberWithInt:[item intValue]];
newFollowing.followerid = itemnum;
newFollowing.user = record; //where record is a user just created
}
【问题讨论】:
-
我有点不清楚您要存储哪些用户和关系。您是否尝试将后端系统中存在的所有用户和关系存储在 Core Data 中?还是只有应用用户的关系?
-
相当多的用户说大约有 20 个推荐用户,以便应用用户有机会关注他们。但就关注关系而言,只有应用用户当前关注的用户或当前关注应用用户的用户。
标签: ios core-data relationship