0

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

我的 Resoucemanager 即 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{
        });
4

1 回答 1

0

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

至少你需要清理它。目前还不清楚为什么AKObjectManager有一个sharedManager方法,如果它是子类的,那么超类不可实例化是合乎逻辑的......

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

于 2015-08-28T08:15:44.267 回答