Managing an enum within a PFObject - iOS Swift/Parse

297 Views Asked by At

I'm new to Parse and Swift.

I have an app where people play against each other.

I want to create an activity feed where game results, cheers, heckles (anything really) can show up in a list.

My thought is to create an Activity class that subclasses PFObject and I'd like to have an enum ActivityType to determine what kind of Activity is being created.

Can I set up the Activity object in Parse and the PFObject in Swift so that each Activity is set up with the correct ActivityType?

My thinking is that I need a "Type" column in Parse that's just a number and an init method that reads that number and sets the correct type.

Does that sound about right?

Thanks

2

There are 2 best solutions below

0
On

You could use a type column in parse, though I'd expect each of your different kinds of feed item to be different classes in parse as they all have different data and relationships, so you could use the class type (name).

In either case this is just a way to identify the type coming from the server. Once you have that you want an organised and common approach to displaying the feed items. To do that you should have a protocol which defines what a feed item needs to provide in order to be displayed on the feed. Then you have a set of classes, each conforming to that protocol, and each dealing with one of the different types of feed item to 'mutate' them into the common format for display.

Using an enum in your app would work, but it could lead you to have one big switch statement dealing with everything. So long as you just use the enum and switch to deal with deciding which class to create to handle the feed item then your code should be well structured.

0
On

I've solved this problem. It was 'free' functionality from Parse. I think first of all, you should consider subclassing PFObject (for many reasons). Once you do this, all you have to do is add the enum as a property to your subclass. It's taken care of automatically by Parse.

Parse knows how to convert to NSNumber and vice versa for an enum, no need to worry about that.

Note, in your .m file:

@implementation MyParseObjectSubclass

@dynamic aPropertyIWantPersisted;  // declare your properties as dynamic to be managed by Parse
@synthesize aLocalTransientProperty;  // if you have transient properties that you don't want persisted to the server.  

+ (void)load
{
   [self registerSubclass];
}
+ (NSString*)parseClassName
{
   return "MySubclass";
}
@end