【问题标题】:Restkit - enqueueObjectRequestOperation not being called after adding rootviewcontroller in appdelegateRestkit - 在 appdelegate 中添加 rootviewcontroller 后未调用 enqueueObjectRequestOperation
【发布时间】:2017-12-30 01:20:35
【问题描述】:

我一直在使用 Restkit 连接到我的 API 服务器,它运行良好。现在我的情况是我在didFinishLaunchingWithOptionsappdelegate.m 中添加了一个检查器。它检查 coredata 中是否已经存在 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

【问题讨论】:

    标签: ios objective-c restkit


    【解决方案1】:

    你为什么打电话?

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

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 2012-11-05
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多