我有两个子类PFObjects如下:
@interface BQQuestion : PFObject<PFSubclassing>
@property (retain) NSString *user;
@property (retain) NSString *questionText;
@property (retain) NSArray *answers; //this is intended to hold an array of BQAnswers
+ (NSString *)parseClassName;
@end
@interface BQAnswer : PFObject<PFSubclassing>
@property (retain) NSString *user;
@property (retain) BQQuestion *question;
@property (retain) NSString *answerText;
@property int votes;
+(NSString *)parseClassName;
@end
我正在尝试使用以下代码将BQAnswers添加到BQQuestion的answers属性中:
BQQuestion *t = [BQQuestion object];
t.questionText = @"Another test";
BQAnswer *one = [BQAnswer object];
one.answerText = @"Very nice.";
BQAnswer *two = [BQAnswer object];
two.answerText = @"Good.";
t.answers = @[one, two];
[t saveInBackground];
t,一个和两个都保存到Parse云中,但是没有为BQQuestion.answers生成列,因此关联不会保存。我在这里弄错了什么?
任何帮助将不胜感激。谢谢!
答案 0 :(得分:1)
指向PFObject数组的指针是PFRelation。为此,您将在Question对象中创建一个包含指向Answer对象的关系的列。我假设您的解析班被称为&#34;问题&#34;并且包含指向答案的关系的添加列称为&#34;答案&#34;
BQQuestion *question = [PFObject objectWithClassName:@"Question"];
PFRelation *answers = [t relationForKey:@"answers"];
for (PFObject *answer in answers) {
[question add:answer];
}
[question saveInBackground];
要检索答案对象:
BQQuestion *question = //assume you have the question object;
PFRelation *answers = [question relationForKey:@"answers"];
PFQuery *query = [answers query];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
// results contains all the answers to that question
}];