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

Restkit - enqueueObjectRequestOperation not being called after adding rootviewcontroller in appdelegate

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

这是我的代码:

在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

你为什么打电话?

[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];