krishna baddi
krishna baddi

Reputation: 1

Conflicting SharedInstance during method call with different instance (RestKit)

I am using Restkit framework for RESTFUL web service.

My Resoucemanager i,e TaskManager and TaskDashboardCountManager are conflicting . When I call method

 [[TaskDashboardCountManager sharedManager] loadTaskCount:^(NSDictionary *task) {}];

I am getting the exception

'NSInvalidArgumentException', reason: '-[TaskDashboardCountManager loadTask:failure:]:

loadTask is the method of TaskManager but it is getting called using TaskDashboardCountManger . how do i solve this ?

 **RKObjectManager.h**
          @interface AKObjectManager : RKObjectManager

            + (instancetype) sharedManager;

            - (void) setupRequestDescriptors;
            - (void) setupResponseDescriptors;

            @end

    **RKObjectManager.m**
    static AKObjectManager *sharedManager = nil;

    @implementation AKObjectManager

    + (instancetype)sharedManager {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSURL *url = [NSURL URLWithString:BASE_DEVELOPMENT_URL];

            sharedManager = [self managerWithBaseURL:url];

            sharedManager.requestSerializationMIMEType = RKMIMETypeJSON;
            /*
             THIS CLASS IS MAIN POINT FOR CUSTOMIZATION:
             - setup HTTP headers that should exist on all HTTP Requests
             - override methods in this class to change default behavior for all HTTP Requests
             - define methods that should be available across all object managers
             */

            [sharedManager setupRequestDescriptors];
            [sharedManager setupResponseDescriptors];

            AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
            [sharedManager.HTTPClient setDefaultHeader:@"Authorization" value: [NSString stringWithFormat:@"token %@", delegate.key]];


        });

        return sharedManager;
    }

    - (void) setupRequestDescriptors {
    }

    - (void) setupResponseDescriptors {
    }


    **TaskManager.m**
    @implementation TaskManager

    + (id)sharedInstance {
        static TaskManager *__sharedInstance=nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            __sharedInstance = [[self alloc] init];

            [__sharedInstance setupResponseDescriptors];
        });

        return __sharedInstance;
    }



    - (void) loadTask:(void (^)(NSArray *task))success failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure{

        });


    **#import "TaskDashboardCountManager.h"**

        static TaskDashboardCountManager *sharedManager = nil;

        @implementation TaskDashboardCountManager

        + (id)sharedInstance {
            static TaskDashboardCountManager *__sharedInstance=nil;
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                __sharedInstance = [[self alloc] init];

                [__sharedInstance setupResponseDescriptors];
            });

            return __sharedInstance;
        }

        - (void) loadTaskCount:(void (^)(NSDictionary *task))success failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure{
        });

Upvotes: 0

Views: 67

Answers (1)

Wain
Wain

Reputation: 119031

You appear to have mixed up sharedInstance and sharedManager. There will be 1 sharedManager which will be the first one created, then, any other reference to sharedManager no matter which class it's from will return the same instance.

At minimum you need to clean this up. It isn't really clear why AKObjectManager has a sharedManager method, if it's subclassed then it's logical for the superclass to not be instantiable...

You're better off not trying to use singletons so pervasively and instead create the instances explicitly and use dependency injection.

Upvotes: 0

Related Questions