karlo jay bueno karlo jay bueno - 1 year ago 63
Objective-C Question

Restkit - enqueueObjectRequestOperation not being called after adding rootviewcontroller in appdelegate

I have been using Restkit to connect to my API server, and it works perfectly. Now my situation is that I added a checker in my

appdelegate.m
in
didFinishLaunchingWithOptions
. it checks if there is already adminID in the coredata. if there was a adminID stored, when the app is relaunched, it will set the rootviewcontroller into a uinavigation controller. but when the uiviewcontroller is called, the Restkit
enqueueObjectRequestOperation
is not called.

Here's my code:

in 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;
}


Here's my code in the Navigation controller's 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];
}


i called the
fetchData
in viewWillAppear.

Then i add a breakpoint to the function, and it goes there, the problem is that the restkit is not calling the enqueueObjectRequestOperation.

Please help!!!

By the way, i make a custom core data entity to store the adminID

Answer Source

Why are you calling?

[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation];

I think you should call

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

If you want to add it to queue of Object Request Operations you should first init RKObjectManager and than call 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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download