Question

In my project I have an object MainAppDataObject that I was to use globally between all other classes. I want to keep other object in it which I need in different viewControls and can be accessed easily. I believe for this, I got to implement Singleton pattern.

I tried implementing in 2 days i.e. using AppDelegate and GlobalObjects, but cannot access in other viewController or can't set the object to be saved.

Using GlobalObjects method : Header file :

@interface MC_GlobalObjects : NSObject

    +(void) load;
    +(MainAppDataObject*) sharedAppDataObject;

@end

Implementation File .m :

#import "MC_GlobalObjects.h"

static MainAppDataObject* _sharedMainAppDataObject = nil;

@implementation MC_GlobalObjects

    +(void) load {
        _sharedMainAppDataObject = [[MainAppDataObject alloc] init];
    }

    +(MainAppDataObject*) sharedAppDataObject {
        return _sharedMainAppDataObject;
    }
@end

In my LoginViewController.m :

#import "MC_GlobalObjects.h"

    MainAppDataObject *sharedAppObj = [MC_GlobalObjects sharedAppDataObject];
    sharedAppObj.authLogin = logAgent;

I got the above reference from here , and I implemented as it says. I import MC_GlobalObjects.h, and just call sharedAppDataObject of MC_GlobalObject class. MC_GlobalObject obj is not initialized anytime nor it's method load is called. This line is the first time that I am using MC_GlobalObjects object from the start of the project. Result of the above code : sharedAppObj is created and it has authLogin as nil (as expected). When I assign logAgent to authLogin, authLogin is created but its contents remains nil. logAgent has values in it like full_name, job_title etc properties.

Can anyone help me know, why the values of logAgent object aren't being stored in authLogin. Then I got to access authLogin in ProfileViewController.

Is anything to correct in MainAppDataObject or AgentDetails obj. MainAppDataObject.h

@interface MainAppDataObject : AppDataObject
{
    AgentDetails *authLogin;
}
@property (nonatomic, copy) AgentDetails *authLogin;

Implementation File : .m

@synthesize authLogin;

AgentDEtails : .h

@interface AgentDetails : NSObject <NSCopying>

    @property (nonatomic, copy) NSString *full_name;
    @property (nonatomic, copy) NSString *job_title;
    @property (nonatomic, copy) NSString *photo;
    @property (nonatomic, copy) NSNumber *agent_id;


    @property (nonatomic, copy) NSString *agentSecret;
@property (nonatomic) bool chatStatus;
    @property (nonatomic) bool onlineStatus;
@property (nonatomic, copy) NSNumber *session;

-(id) initWithNSDictionary: (NSDictionary *)loginInfo;

@end

Implementation .m :

@synthesize full_name;
@synthesize job_title;

Where is the problem ? Can anyone please help me sort out. I got access such many objects and data across many viewControllers and other objects. Any help is highly appreciated. Thanks a lot. I have also referred other questions on the site referring the same query, but can't find a solution for the problem that I am facing. I have look around at [here], here2, here, here, etc.

Was it helpful?

Solution

A couple of things. First, there's no reason to have a separate MC_GlobalObjects and MainAppDataObject classes. Make your MainAppDataObject object the singleton.

Second, you don't need to, and should not, have a +load method. Make the sharedAppDataObject method lazily load the object if it isn't loaded already:

@interface MainAppDataObject: NSObject

@property (nonatomic, strong) NSObject *someObject;
@property (nonatomic, strong) NSObject *someOtherObject;
@property (nonatomic, strong) NSString *someString;

+(MainAppDataObject) sharedAppDataObject;

@end

And your .m file:

static MainAppDataObject* _sharedAppDataObject = nil;

@implementation

+(MainAppDataObject) sharedAppDataObject;
{
  if (_sharedAppDataObject == nil)
    _sharedAppDataObject = [[MainAppDataObject alloc] init];
  return _sharedAppDataObject;
}

And then to use it in any class in your project, first #import the header, then use code like this:

[MainAppDataObject sharedAppDataObject].someString = @"Some string";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top