Restkit - 在appdelegate中添加rootviewcontroller后未调用enqueueObjectRequestOperation

时间:2017-07-24 12:38:28

标签: ios objective-c restkit

我一直在使用Restkit连接到我的API服务器,它运行得很好。现在我的情况是我在appdelegate.m didFinishLaunchingWithOptions中添加了一个检查器。它会检查coredata中是否已有adminID。如果存储了adminID,则当重新启动应用程序时,它会将rootviewcontroller设置为uinavigation控制器。但是当调用uiviewcontroller时,不会调用Restkit enqueueObjectRequestOperation

这是我的代码:

在appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //adminID is fetch from custom Entity in core data
    if(adminID==0){
        AllKommunScreenViewController  *adminController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"AllKommunScreenID"];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:adminController];

        self.window.rootViewController = navController;
    }
    return YES;
}

这是我在导航控制器的Rootview中的代码:

-(void)fetchData{
    RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Auth class]];
    [responseMapping addAttributeMappingsFromArray:@[@"status", @"sucess", @"data"]];

    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

    RKResponseDescriptor *userProfileDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:statusCodes];

    NSString *apiPath = [NSString stringWithFormat:@"%@%@", baseURL,@"/api/kommun/all/stats/"];

     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:apiPath]];
    request setValue:_sharedManager.userAccessToken forHTTPHeaderField:@"X-ACCESS-TOKEN"];

    RKObjectRequestOperation *operation =[[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[userProfileDescriptor]];

    [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {} failure:^(RKObjectRequestOperation *operation, NSError *error) {}];

    [RKObjectManager sharedManager] enqueueObjectRequestOperation:operation];
}

我在viewWillAppear中调用了fetchData

然后我在函数中添加一个断点,它就到了那里,问题是restkit没有调用enqueueObjectRequestOperation。

请帮助!!!

顺便说一下,我创建了一个自定义核心数据实体来存储adminID

1 个答案:

答案 0 :(得分:0)

你为什么打电话?

[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation];

我认为你应该致电

RKObjectRequestOperation *operation =[[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[userProfileDescriptor]];

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

//print something 
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// print something
}];

[operation start];

如果要将其添加到对象请求操作队列中,首先应该初始化RKObjectManager,然后调用enqueueObjectRequestOperation:

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:baseUrl];

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:apiPath]];
request setValue:_sharedManager.userAccessToken forHTTPHeaderField:@"X-ACCESS-TOKEN"]; 
RKObjectRequestOperation *operation =[[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[userProfileDescriptor]];

[manager enqueueObjectRequestOperation:operation];