Ken M. Haggerty
Ken M. Haggerty

Reputation: 26198

Parse: Create 1-to-1 relation on PFObject with support for multiple class types?

I am trying to create a PFObject subclass in Parse that has a one-to-one relation to another PFObject in Parse, but I would like this relation to support any PFObject subclass. Currently, as soon as I create and save my first PFObject to Parse, whatever that PFObject is connected to is set as that relation's one and only accepted type, and creating a second object that connects to a different PFObject subclass for that relation generates an error.

Is there a way either to...

Example:

For example, let's say I needed to create a subclass of PFObject called Zookeeper in addition to multiple subclasses of PFObject for various types of animals: Armadillo, Bear, Camel, etc. (and for reasons that we must accept, each animal has to have its own PFObject subclass rather than using a generic Animal class).

Then let's say that each Zookeeper is responsible for exactly one animal. To do this, I want to create a one-to-one relation on Zookeeper called animal so that I can specify a single animal object that each Zookeeper is responsible for.

I create multiple Zookeeper and multiple animals. I successfully save my first animal (an Armadillo) and my first Zookeeper object ("Alice") and my second animal (a Bear), but when I try to save my second Zookeeper object ("Bill") to Parse, I get the following error:

invalid type for key animal, expected *Armadillo, but got *Bear

Upvotes: 0

Views: 274

Answers (2)

Ken M. Haggerty
Ken M. Haggerty

Reputation: 26198

What I ended up doing was saving the class name and object ID of the desired target object to my object rather than saving it as a pointer; so in the example above, that would mean creating the properties animalType and animalID on Zookeeper and using those properties to query the relevant animal with objectId = animalID. That way I don't have to create any new class types that are purely relational.

Upvotes: 0

Josh Gafni
Josh Gafni

Reputation: 2881

I am not sure if this is exactly what you want, but could you:

  1. Create an Animal subclass in Parse, with a text field called "type", and one column for each of the different types of animals (yeah, I know this is isn't that elegant).

  2. The column matching the "type" column contains a pointer to the specific animal object, while the other columns are nil.

  3. To get the specific animal object, you need to get the Animal object, look at the type column, and then look at the corresponding pointer column to get the specific animal object.

For example, the Zookeeper object contains a pointer to an Animal object. The "type" column of Animal object contains the text "bear". You then retrieve the object in the "bear" column of the Animal object, which is a Bear object. Does this meet your specification? Yes there is an Animal object, but there is still a notion of specific animal objects like Bear, etc.

Upvotes: 1

Related Questions