简体   繁体   English

两个PFObject可以互相指向吗?

[英]Can two PFObjects point at each other?

I have two PFObject, I'll call them A and B for simplicity. 我有两个PFObject,为了简单起见,我将它们称为A和B.

  1. I create A and save it. 我创建A并保存它。
  2. I create B, set a property on it called "a" pointing to A and save B 我创建B,在其上设置一个名为“a”的属性,指向A并保存B.
  3. I then create a property on A called "b" and then save A 然后我在A上创建一个名为“b”的属性,然后保存A.

When I try to do that something does not seem to work right, I am trying to understand if it's something else in my code or if Parse does not allow me to have two PFObjects that point at each other. 当我尝试做某事似乎不正常时,我试图理解它是否是我的代码中的其他内容,或者Parse是否允许我有两个指向彼此的PFObject。

Can two PFObjects point at each other? 两个PFObject可以互相指向吗?

They can indeed. 他们确实可以。 I am assuming that you're not waiting for the save to complete, and it fails because of that. 我假设您没有等待保存完成,因此失败了。

If you create A and save it, create B pointing to A and save B, then you can point to B on A and save that too. 如果你创建A并保存它,创建B指向A并保存B,那么你可以指向A上的B并保存它。 The saves must be complete though, in between operations. 在操作之间,必须完成保存。

Regardless, it's not a recommended approach. 无论如何,这不是推荐的方法。

PFObject *post = [PFObject objectWithClassName:@"Post"];
PFObject *comment = [PFObject objectWithClassName:@"Comment"];
[post saveInBackgroundWithBlock:^(BOOL success, NSError *error) {
  [comment setObject:post forKey:@"post"];
  [comment saveInBackgroundWithBlock:^(BOOL success, NSError *error) {
    [post setObject:comment forKey:@"comment"];
    [post saveInBackgroundWithBlock:^(BOOL success, NSError *error) {
      // Both objects should be linked now.
    }];
  }];
}];

They can't (as far as I know). 他们不能(据我所知)。

I came across the same issue. 我遇到了同样的问题。 My workaround was to have B store the objectId of A, and A store a pointer to B. 我的解决方法是让B存储A的objectId ,A存储指向B的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM