简体   繁体   中英

Can two PFObjects point at each other?

I have two PFObject, I'll call them A and B for simplicity.

  1. I create A and save it.
  2. I create B, set a property on it called "a" pointing to A and save B
  3. I then create a property on A called "b" and then save 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.

Can two PFObjects point at each other?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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