rfj001
rfj001

Reputation: 8518

Parse Abstract PFObject with Two Subclasses

I am using Parse as my backend, and I am trying to have two subclasses map to the same table.

In my models, I have an abstract Post class, which conforms to the PFSubclassing protocol and contains several properties and methods. In my Parse database, I want to have a Post table.

Here is some of my Post.h file, to give an idea:

@interface ParsePost : PFObject<PFSubclassing>

@property (strong, nonatomic) PFUser *postedBy;

@property (strong, nonatomic) NSString *text;

@property (strong, nonatomic) PFGeoPoint *location;

@property (strong, nonatomic) PFFile *image;
@property (strong, nonatomic) PFFile *thumbnail;

@property (strong, nonatomic) NSNumber *commentCount;

Now, in my source code I want a Message and Comment class, representing the two types of posts that can be made. Messages and Comments share many properties and methods, but also have some differences as well. I had hoped that I could subclass Post essentially without Parse even knowing, and just use the different concrete subclasses in my source code to simplify development and application logic... but Parse is giving me some errors.

Here is Message.h (empty)

@interface Message : ParsePost


@end

And Comment.h

@interface Comment : ParsePost

@property (strong, nonatomic) ParsePost *parentPost;

@end

The implementation files contain some more differences, but that shouldn't be important to the problem (I don't think). Neither override Post's + (NSString *)parseClassName method, which seems to be the most important part of PFSubclassing.

Things were working for me like I had hoped, but then I updated my Parse framework and switched to CocoaPods instead of manually adding the framework, and now I am getting a few errors...

The first thing I got, when I tried to build my program was an error saying 'The class Message must be registered with registerSubclass before using Parse.' Now, since Message inherits from Post and Post registers as a subclass, I wouldn't have expected this... but I get the issue nonetheless.

So blindly, not really understanding and just hoping things would work out, I added + (void)load { [self registerSubclass]; } to both Message.m and Comment.m. Now, however, I'm getting an error saying 'Tried to register both Comment and Message as the native PFObject subclass of Post. Cannot determine the right class to use because neither inherits from the other.' This message does not really make sense to me either...

If I only register Message then the app loads fine... but I am afraid that will lead to trouble down the road.

Would someone please explain to me what is going on here? Why was this working before and all of a sudden is throwing errors at me? I don't want Parse to even worry about the fact that I subclass Post... It should just worry about the Post objects that I retrieve from and send to the server, no matter what names and methods I give to the local objects on the client side.... right?

Upvotes: 1

Views: 238

Answers (1)

Wain
Wain

Reputation: 119041

The problem is that parse SDK offers to create instances of your subclass when you ask it to download content so it needs to know the unique class type associated with the class in the cloud DB. If you try to register multiple classes it gets confused because you're asking for something unexpected.

A better option would be to maintain only a single Post class with all of the required properties, and then to add categories which expose special properties of a particular subclass / type and associated methods for convenience creation.

This is effectively just allowing you to use the comment and message terminology while allowing parse to work in terms of Posts.

Upvotes: 1

Related Questions