Restkit-enqueueObjectRequestOperation在appdelegate中添加rootviewcontroller之后不会被调用
问题描述:
我一直在使用Restkit连接到我的API服务器,并且它完美地工作。现在我的情况是我在我的appdelegate.m
的didFinishLaunchingWithOptions
中添加了一个检查器。它会检查coredata中是否已经存在adminID。如果存储了adminID,当应用程序重新启动时,它会将rootviewcontroller设置为uinavigation控制器。但是当调用uiviewcontroller时,不会调用Restkit enqueueObjectRequestOperation
。Restkit-enqueueObjectRequestOperation在appdelegate中添加rootviewcontroller之后不会被调用
这里是我的代码:
在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];