方法调用期间与不同实例(RestKit)冲突的SharedInstance

时间:2015-08-28 06:27:43

标签: ios iphone ios8 restkit-0.20 restkit-0.24.x

我正在使用Restkit框架进行RESTFUL Web服务。

My Resoucemanager i,e TaskManager和TaskDashboardCountManager是冲突的。 当我打电话给方法时

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

我收到了异常

  

' NSInvalidArgumentException',原因:' - [TaskDashboardCountManager loadTask:failure:]:

loadTask是TaskManager的方法,但它是使用TaskDashboardCountManger调用的。我该如何解决这个问题?

 **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{
        });

1 个答案:

答案 0 :(得分:0)

您似乎混淆了sharedInstancesharedManager。将会有1 sharedManager这将是第一个创建的sharedManager,然后,对AKObjectManager的任何其他引用,无论它来自哪个类都将返回相同的实例。

至少你需要清理它。不清楚为什么sharedManagerremoveUrl("http://go.com\\\\")方法,如果它是子类,那么超类的逻辑是不可实例化的......

你最好不要如此普遍地使用单例,而是明确地创建实例并使用依赖注入。