I have an existing model class which I would like to manage with Parse, ideally by changing its superclass from NSObject to PFObject. But this is a real-world model class, designed without Parse in mind, so it presents a number of challenges.
challenges
The challenges are:
- This class is in Swift.
- This class has stored properties and computed properties. (So Parse should only manage the stored properties, of course.)
- Some properties hold arrays of instances of other custom model classes
- Some of those custom classes themselves hold User objects, which should be managed by Parse.
Every one of these things seems to require special treatment to be handled correctly in Parse, and Parse’s documentation is brief on these issues.
existing model class hierarchy
To give an example, my class is something like this:
// I’d like to change this to be a PFUser subclass.
class User : NSObject { /* an ordinary user object */ }
// And this to be a PFObject subclass
class Tango : NSObject {
var user:User // Presumably, a Parse "Pointer" to a PFUser
var opponent:Participant
var info:NSString?
var watchers:NSArray // an array of Participants
var duration:NSTimeInterval? // (notice this is an optional value type, in accessible from Objc)
// various methods
}
// a custom class which contains a User
class Participant : NSObject
{
var status:String
var user:User // a Pointer to a PFUser?
// etc..
}
key questions
So I have a few key questions here:
- Should all references to User be replaced with
Pointer
s toUser
a subclass ofPFUser
? - Do I need to replace Participant with a plain old Cocoa type like NSDictionary? Or is there a way to provide interconversion with NSDictionary only for the purposes of Parse’s internal serialization?
- Is there any provision for swift-only types like the
Optional<NSTimeInterval>
? Or is this another case where there’s an extension point to provide inter-conversion with a data type Parse can handle? - How to model the array of Participants? the User objects within the Participants?
- Given all these apparent difficulties with
Tango
, would I be wiser just to implement a method for converting an instance to/from plain old plist types (dictionaries, arrays, strings, numbers), and have a PFObject handle those?
I am not interested in maximal query flexibility or maximal performance. I am interested in the quickest thing which can work reliably, and which requires the minimum re-writing of existing code that depends on the existing class hierarchy. I am aware that the User / Tango
relationship could be described as many-to-many, which is what PFRelation
is for, but I have the impression it would require a major redesign to use it.
In other words, I am really hoping I can avoid replacing my entire model layer with a new class hierarchy designed as denormalized tables for for Parse's pleasure. I have existing code which works with the current structure, and I am trying to fit in Parse without breaking anything already there.
So are these key questions the right ones to retrofit this class for Parse? What is the right way to handle them?