簡體   English   中英

當我只允許保存當前登錄的用戶時,如何為用戶創建反向關系?

[英]How do I create an inverse relationship for a User when I'm only allowed to save the currently logged in user?

我的User類與Convos類有多對多的關系。 我想將user (這是一個不是當前登錄用戶PFUser * )添加到convo對象,我想將convo添加到user對象以獲得反向關系。

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

[self.convo save];

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

問題是我不允許保存user因為顯然我只能保存當前登錄的用戶。 這是錯誤:

除非已通過logIn或signUp對用戶進行身份驗證,否則無法保存用戶

我該如何解決這個問題?

我能想到的最簡單方法是擁有一個擁有Convos關系的獨立PFObject。 例如

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]];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM