0

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.

Community
  • 1
  • 1
Tvd
  • 4,463
  • 18
  • 79
  • 125
  • See http://stackoverflow.com/questions/5720029/create-singleton-using-gcds-dispatch-once-in-objective-c?rq=1 for a far superior way to setup a singleton. – rmaddy May 10 '14 at 18:00
  • @rmaddy, Thanks. Can implement that in my other class where I access API's. But that doesn't give a solution for this problem. But it definitely helps in something else. Thanks for this quick answer. – Tvd May 10 '14 at 18:14

1 Answers1

1

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";
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thanks a lot. I have 1 more query. I have a class APIUtility - it has methods thru which I access API's. In each view I need access to this class. It just has a key variable that is set at initial stage of app & is used to access api's. My Query - I am thinking to making this class also singleton so I don't need to create its obj in each view & also not set the key every time. Will it be advisable to create this as singleton or create a new obj in each view passing/setting the key (stored in MainApp.. probably) every time obj is created. What would be a better option. Pls Suggest – Tvd May 12 '14 at 08:36
  • 1
    Sounds like another good situation for a singleton. Give it an instance variable for the key. Jse the same pattern I outlined above, where it has a static variable to save the single instance and return it. – Duncan C May 12 '14 at 11:46
  • Thanks a lot. This helped me decide what to do and proceed ahead. Once again Thanks. – Tvd May 12 '14 at 11:49