3
votes

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?

3

3 Answers

1
votes

How does your custom class defined? Because their subclassing mechanism is relying on Key-Value-Coding(KVC). Here's KVC Programming Guide. Or you can check out the guide over here.

The following sample should work.


In the header:

@interface CustomObject : PFObject <PFSubclassing>

@property (nonatomic, strong) NSString *propertyName;

In the implementation:

#import <Parse/PFObject+Subclass.h>

#pragma mark - columns of Parse table
@dynamic propertyName;

#pragma mark - regiester parse subclass in runtime
+ (void)load {
    @autoreleasepool {
        [self registerSubclass];
    }
}

#pragma mark - parse cloud table name
+ (NSString *)parseClassName
{
    return @"CustomObject"; // table name
}
1
votes

TL;DR: Make sure your subclass of PFObject is registered with Parse by the time your Cloud code returns those objects from a net call.

If you've subclassed PFObject using the instructions Parse includes in it's documentation guide, you can just make the following call before the first net call that returns your custom subclass:

CustomSubclass.initialize()

A good place to add this code is in your app delegate's application(didFinishLaunchingWithOptions:) method, where it will be likely to run before any other code related to your subclass.


Long version with explanation

My situation was a little different from yours, but I believe the problem is largely the same. My problem was that I was fetching objects through a PFQuery and the objects returned in the query callback were just generic PFObjects.

Here's what my code looked like:

// Scene is a PFObject subclass func getLatestScenes(completion: ((scenes: [Scene]) -> Void)?) { var query = PFQuery(className: SceneClassName) query.findObjectsInBackgroundWithBlock { (results: [AnyObject]?, error: NSError?) -> Void in if let scenes = results as? [Scene] { // This would never run, as the members of `results` were only PFObjects completion?(scenes: scenes) } else { // Code would always skip to the empty result case completion?(scenes: []) } } }

Individual members of the results array would print out in the debugger with descriptions that clearly stated that they were supposed to be of the class Scene, as intended, but when inspected they were only PFObjects.

(lldb) po results![0] <Scene: 0x7fc743a8f310, objectId: YyxYH9dtBp, localId: (null)> { creator = "<PFUser: 0x7fc74144b320, objectId: FV3cmDI1PW>"; sceneDataFile = "<PFFile: 0x7fc743a96920>"; } (lldb) p results![0] (PFObject) $R6 = 0x00007fc743a8f310 { NSObject = { isa = 0x00007fc743a8f310 } ... // Info on object... ... }

The problem here was that the Scene object was not properly registered with Parse as a PFObject subclass. The initialize method was correctly overridden like so:

override class func initialize() { struct Static { static var onceToken : dispatch_once_t = 0; } dispatch_once(&Static.onceToken) { self.registerSubclass() } }

but the initalize function wasn't being called by the time the query was called. The issue is that a class's initialize method is called right before the first message is sent to that class, but there was no message being sent to Scene before the query was performed. Adding the following line to my AppDelegate's application(didFinishLaunchingWithOptions:) solved the issue

Scene.initialize()

After that, Parse was able to infer which class on the client the incoming data the objects should be, and constructed the results array with that class.

0
votes

It's simple after all. What worked for me is a simple assignment of the returned PFObject to the PFObject's Objective-C subclass. So in your case,

[PFCloud callFunctionInBackground:@"func" withParameters:@{} block:^(id objects, NSError *error) {

  for (PFObject *object in objects) {

    MediaFile *mf = object; // simple as that

    [array addObject:mf];
  }
}];