2

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?

pizzafilms
  • 3,829
  • 4
  • 24
  • 39
  • 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 Nov 13 '14 at 18:42
  • It's not [myThing object], it's [CustomClass object]...which creates and allocates a new custom PFObject subclass. – pizzafilms Nov 13 '14 at 22:08
  • 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 Nov 13 '14 at 22:22
  • 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 Nov 14 '14 at 02:36
  • Take a look at my answer here: http://stackoverflow.com/questions/26381666/how-can-i-automatically-initialize-mutable-arrays-dictionaries-for-my-pfobject/26409127#26409127 – LostInTheTrees Nov 14 '14 at 15:53
  • @LostInTheTrees I'm unclear (from your answer) how that answers THIS question. The OP wants an automatic way to initialize **query-generated** instances of his class. He's already figured out how to initialize instances he makes on his side. Am I missing something? – mbm29414 Nov 14 '14 at 17:53
  • I thought he would find that code more useful than his rudimentary "init". "Initializing" an already existing object doesn't seem to make much sense. My tLObjectInit function can be called on any object. I could contain any number of entries of the form "if () initialize the field". – LostInTheTrees Nov 14 '14 at 21:36
  • @LostInTheTrees while I appreciate your UUID idea, which is useful, it doesn't answer what I'm looking for...a way to initialize a PFObject whether it's created from scratch or from a query. It's unfortunate that Parse won't let us override init or at least give us parameters to be able to override it. That would be an answer. – pizzafilms Nov 15 '14 at 01:13
  • 1
    @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 Nov 17 '14 at 16:30

1 Answers1

1

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];
}
Zhanserik Kenes
  • 335
  • 2
  • 7