3

我的 User 类与 Convos 类具有多对多的关系。我想将userPFUser *不是当前登录的用户)添加到convo对象,并且我想添加convouser对象以实现逆关系。

PFRelation *relation = [self.convo relationforKey:@"users"];
[relation addObject:user];

[self.convo save];

PFRelation *inverseRelation = [user relationforKey:@"convos"];
[inverseRelation addObject:self.convo];
[user save];

问题是我不允许保存user,因为显然我只能保存当前登录的用户。这是错误:

除非已通过登录或注册进行身份验证,否则无法保存用户

我该如何解决这个问题?

4

1 回答 1

2

我能想到的最简单的方法是拥有一个单独的 PFObject ,它拥有 Convos 关系。例如

PFUser["proxy"] -> PFObject["convos"] -> PFRelation

您的代码将变为:

PFObject *relation = [self.convo relationforKey:@"users"];
[relation addObject:user];

PFObject *userProxy = [user objectForKey:@"proxy"];

// N.B. You need to make sure this object exists already, and create it if not
// N.B. Make sure you include this key when you query for the user, or you'll need to load the data here
if (userProxy == nil) NSAssert(nil, @"Need to create the users Proxy object");
if (userProxy.isDataAvailable == NO) NSAssert(nil, @"Need to include the Proxy object key in the user query");


PFRelation *inverseRelation = [userProxy relationforKey:@"convos"];
[inverseRelation addObject:self.convo];

[PFObject saveObjects:@[self.convo, userProxy]];
于 2013-04-23T03:40:58.147 回答