2
votes

The Parse docs specifically state that you should not override init for subclasses of PFObject. That's okay for objects that are created in code because you can override the object method, like this...

@implementaion CustomObject : PFObject <Subclassing>

+ (instancetype) object {
    CustomObject *me = [super object];

    // init some instance variables or whatever

    return me;
}

@end

This works when for this case: CustomObject *myThing = [CustomObject object];

But object does not seem to be called when fetching an object from a query....

PFQuery *query = [CustomObject query];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        for(int i = 0; i < objects.count; i++){
            CustomObject *myThing = objects[i];
            // object method was never called...
        }
    }
}];

So...how can I initialize a custom PFObject when it's fetched?

1
First, what instance variables are you instantiating that make sense on the iOS client only? Second, what if, after retrieving the object from the array, you simply call [myThing object]? A bit redundant, but it would work, right? - mbm29414
It's not [myThing object], it's [CustomClass object]...which creates and allocates a new custom PFObject subclass. - pizzafilms
Ah, my bad. I missed that it's a class method. Well, is it possible to move the initialization code to its own method, call it from [MyObject object], but also be able to call it on an object returned from a PFQuery? - mbm29414
I guess that's possible, it's just that I'd like to have it in something like an init method where it would get called without me having to think about it. - pizzafilms
@pizzafilms I don't understand how an object can be created by a query. It has to have already been created to be found by a query. In that case, it should have already been initialized. Are you trying to repair previous incorrect initializations? - LostInTheTrees

1 Answers

1
votes

Probably you have not registered your subclass.

Just call [CustomObject register] before

[Parse setApplicationId:@"your id"
                  clientKey:@"your key"];

or add to subclass

+ (void) load{
    [self register];
}