0

I created a custom NSObject subclass so I could call something like MyObject1 *shared = [GlobalObjects myObject1]; as referenced in the accepted answer here: where to store "global" objects in iOS

I have it setup, and it's making the call correctly, but for some reason it's not allowing me to access PFUser *currentUser = [PFUser currentUser] as it's returning null. Right before I make my call for the GlobalObjects myObject1 I am able to access the user details with no problem, but as soon as it jumps to my subclass, it's null. I've included the Parse sdk and there are no errors, so I can't figure out what's going on here.

Anyone have guidance on what I might be doing wrong?

For reference:

Initial view:

currentUser = [PFUser currentUser];
NSLog(@"%@",currentUser); // Outputs correctly
PFObject *settings = [GlobalObjects getSettings];

.h

#import <Foundation/Foundation.h>
#import <Parse/Parse.h>

@interface GlobalObjects : NSObject

+(void)load;
+(PFObject*)getSettings;

@end

.m

#import "GlobalObjects.h"
#import <Parse/Parse.h>

static PFObject* _getSettings = nil;

@implementation GlobalObjects

+(void)load {
    PFUser *currentUser = [PFUser currentUser];
    NSLog(@"%@",currentUser); // Returns null always (I need to get the user here)
}
+(PFObject*)getSettings {
    return _getSettings;
}

@end
Community
  • 1
  • 1
Jake Lisby
  • 494
  • 5
  • 21

1 Answers1

2

As for me your problem - in +(void)load method. From the documentation to +[NSObject load] - order of initialization:

  1. All initializers in any framework you link to.
  2. All +load methods in your image.
  3. All C++ static initializers and C/C++ attribute(constructor) functions in your image.
  4. All initializers in frameworks that link to you.

So you is trying to get PFUser instance on step 2 but Parse framework will be proper initialized only on step 4. So just move your call from the load method to somewhere.

toohtik
  • 1,892
  • 11
  • 27