2017-07-24 68 views
0

我一直在使用Restkit連接到我的API服務器,並且它完美地工作。現在我的情況是我在我的appdelegate.mdidFinishLaunchingWithOptions中添加了一個檢查器。它會檢查coredata中是否已經存在adminID。如果存儲了adminID,當應用程序重新啓動時,它會將rootviewcontroller設置爲uinavigation控制器。但是當調用uiviewcontroller時,不會調用Restkit enqueueObjectRequestOperationRestkit-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

回答

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]; 
相關問題