I have a PFObject subclass called MediaFile. In order to get back instances of my subclass from calls to the server, I've seen how you can construct queries out of Parse subclasses like this:
PFQuery *query = [MediaFile query];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { ... }];
However, most of the server calls in my app are custom Cloud calls, which return PFObjects. As soon as I've retrieved these objects I want to treat them as MediaFile objects and call custom methods on them that are defined in the MediaFile class. Casting does not work, because it does not actually construct a MediaFile object. What I've done to overcome this problem is construct new MediaFiles for each PFObject I get back using [MediaFile object], and then copy all the data into each one using a method I wrote called loadFromObject::
[PFCloud callFunctionInBackground:@"func" withParameters:@{} block:^(id objects, NSError *error) {
for (PFObject *object in objects) {
MediaFile *mf = [[MediaFile object] loadFromObject:object];
[array addObject:mf];
}
}];
In MediaFile.m:
- (MediaFile *) loadFromObject:(PFObject *)object {
NSArray *keys = [object allKeys];
for (NSString *key in keys) {
id obj = [object objectForKey:key];
[self setObject:obj forKey:key];
}
return self;
}
This works, but is there a more elegant way to address this problem?