I am using AFnetworking for authentication, but after Authenticating user I need to move to the next View controller. It's moving, but also it moves when there's an error too. How can I make use of the responseObject in AFNetworking to my need...... Below is my CODE
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
//[operation setCredential:credential];
[operation setResponseSerializer:[AFJSONResponseSerializer alloc]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//This is where if the response is successful it should move
if ([responseObject objectForKey:#"Success"]) {
MainView *home = [self.storyboard instantiateViewControllerWithIdentifier:#"MainViewController"];
[self.navigationController pushViewController:home animated:YES];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Failure: %#", error);
}];
[manager.operationQueue addOperation:operation];
To check if success or not, you are using :
if ([responseObject objectForKey:#"Success"])
I don't know your service and the retrieve Diccionary, however I think you always have an object for the key #"Success", and because #"Success" exists is passing in all the scenarios.
Try to check the value, e.g.:
if ([[responseObject objectForKey:#"Success"] isEqualsToString #"ok"])
Related
My scene is like this, first I have a server json api which return some data for specify page, the api is like /data/page/1. For this case, suppose the response data is :
page 1 => ['a','b']
page 2 => ['c','d']
page 3 => ['e','f']
I use AFNetworking 2 to fetch data from api, for single page data request it works well.
The problem is now I want to implement parallel request for more than one page. I need one api for view controller which accept one pages number, and callback with all data for these pages collected. The api I need is:
typedef void (^DataBlock)(id data);
- (void) dataForPages:(NSInteger)pages withSuccessBlock:(DataBlock)block;
If view controller pass 3 for pages parameter, I want AFNetworking can request data parallel and then collected the 3 result then use in callback block.
I tried to use NSOperationQueue to process multi AFHTTPRequestOperation but failed, the code demo is like this:
- (void) dataForPages:(NSInteger)pages withSuccessBlock:(DataBlock)block
{
//want to use for each, here suppose pages is 3
NSMutableArray *result = [[NSMutableArray alloc] init];
AFHTTPRequestOperation *op1 = [[AFHTTPRequestOperation alloc] initWithRequest:#"/data/page/1"];
AFHTTPRequestOperation *op2 = [[AFHTTPRequestOperation alloc] initWithRequest:#"/data/page/2"];
AFHTTPRequestOperation *op3 = [[AFHTTPRequestOperation alloc] initWithRequest:#"/data/page/3"];
[op1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[result addObjectsFromArray: responseObject]; //responseObject is ['a', 'b']
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[op2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[result addObjectsFromArray: responseObject]; //responseObject is ['c', 'd']
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[op3 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[result addObjectsFromArray: responseObject]; //responseObject is ['e', 'f']
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
NSOperationQueue *q = [[NSOperationQueue alloc] init];
[q addOperation:op1];
[q addOperation:op2];
[q addOperation:op3];
[q waitUntilAllOperationsAreFinished];
block(result);
}
In my test the result always empty, I'm not quite understand waitUntilAllOperationsAreFinished.
Anyone knows how to deal this problem with NSOperation or GCD?
After some code research, I found it's difficult to get what I want with NSOperation and NSOperationQueue, because AFNetworking has it's own completion block handler.
The final solution is use dispatch_group, all code is like this:
dispatch_group_t group = dispatch_group_create();
NSURLRequest *req1 = ...;
NSURLRequest *req2 = ...;
AFHTTPRequestOperation *op1 = [[AFHTTPRequestOperation alloc] initWithRequest:req1];
AFHTTPRequestOperation *op2 = [[AFHTTPRequestOperation alloc] initWithRequest:req2];
NSMutableArray *result = [[NSMutableArray alloc] init];
dispatch_group_enter(group); //enter group
[op1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[result addObjectsFromArray: responseObject];
dispatch_group_leave(group); //leave group in completion handler
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
dispatch_group_leave(group);
}];
dispatch_group_enter(group);
[op2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[result addObjectsFromArray: responseObject];
dispatch_group_leave(group);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
dispatch_group_leave(group);
}];
[op1 start];
[op2 start];
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
block(result);
});
I have a class method containing a block, of AFNetworking in which i want to return one dictionary variable, code shown below:
+(NSMutableDictionary*) getFromUrl:(NSString*)url parametersPassed:(NSDictionary*)parameters;
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
__block NSMutableDictionary *resultarray;
[manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"JSON: %#", responseObject);
NSMutableDictionary *resultarrayTwo = (NSMutableDictionary *)responseObject;
resultarray = resultarrayTwo;
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"Error: %#, %#", error, operation.responseString);
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Message" message:#"Try again" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
}];
return resultArray;
}
How can i return resultArray here, it returns nothing here due to the difference in control flow.
I don't have much knowledge in Objective C block. Waiting for your help.
Thank you.
Change your function design to the following function using Completion Blocks
+(void)getFromUrl:(NSString*)url parametersPassed:(NSDictionary*)parameters completion:(void (^) (NSMutableArray *values))completion;
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"JSON: %#", responseObject);
NSMutableDictionary *resultarray = (NSMutableDictionary *)responseObject;
completion(resultarray);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"Error: %#, %#", error, operation.responseString);
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Message" message:#"Try again" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
completion(nil);
}];
}
And call the function
[YourClass getFromUrl:url parametersPassed:params completion:^(NSMutableArray *values) {
NSLog(#"%#",values);
}];
Update
Removed the extra array used.
Hope this helps.
The network call is asynchronous in nature, so having this method return its result isn't probably the right way of thinking as it implies you do a synchronous network call.
Have the method take a block parameter instead, and execute that block with the result at a later point of time from the AFNetworking completion block.
Before getting the response for GET call, it will execute the next lines. Thats why you are getting no data into Array.
You can call delegate method in success and failure block. This is the best solution.
I understand that AFNetworking has a function that caches an image as it is loaded to an ImageView.
However, I want to cache the Image without displaying it in an ImageView and I was unable to find any functions for that.
Use [NSBundle mainBundle] resourcePath] to get cache path then use NSFileManager and write file/object/doc etc.
Something like (Objective C not Swift - but you get the idea)
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[self savePNGImage:responseObject withFilename:imageUrl];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"%#", [error description]);
}];
[requestOperation start];
Implement savePNGImage to save your image locally.
Hello I want to get result from asynchronous block from the class DataViewControllerto use it in the class MagazineViewControllerbut I don't get the value of returnedDataunless the ViewDidLoadmethod is called for the second time or more as the operation is asynchronous, I know that I have to implement a completion block and call it but I don't know exactly how to do it, this is my code, how can I edit it to give me the value returned in the setCompletionBlockWithSuccessblock and use it in other classes
DataViewController.m
- (void)getDataFromServer:(NSString *)urlString completion:(void (^)(AFHTTPRequestOperation *operation, id responseObject))completion {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURLCredential *credential = [NSURLCredential credentialWithUser:userName password:Password persistence:NSURLCredentialPersistenceNone];
NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:#"GET" URLString:urlString parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCredential:credential];
[operation setResponseSerializer:[AFJSONResponseSerializer alloc]];
[operation setCompletionBlockWithSuccess:completion failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Failure: %#", error);
} ];
[manager.operationQueue addOperation:operation];
}
The MagazineViewController class (Where I want to use the returnedData)
#implementation MagazineViewController
void (^completion)(AFHTTPRequestOperation* , id) = ^(AFHTTPRequestOperation *operation, id responseObject){
returnedData = responseObject;};
- (void)viewDidLoad {
[super viewDidLoad];
DataViewController *dataViewController = [[DataViewController alloc] init];
[dataViewController getDataFromServer:#"http://firstluxe.com/api/search/search?query=vogue&language=1&output_format=JSON" completion:completion];
NSLog(#"returned %# ", returnedData); // here I get the value after the view controller is loaded for the second time or more
You should define completion inline. Your first NSLog statement is called before completion has been called, so the returnedData variable has not been set.
#implementation MagazineViewController
- (void)viewDidLoad {
[super viewDidLoad];
DataViewController *dataViewController = [[DataViewController alloc] init];
[dataViewController getDataFromServer:#"http://firstluxe.com/api/search/search?query=vogue&language=1&output_format=JSON"
completion:^ (AFHTTPRequestOperation *operation, id responseObject) {
returnedData = responseObject;
NSLog(#"returned %# ", returnedData);
}];
}
#end
[self getDataFromServer:#"API URL" completion:^(int *operation, id responseObject) {
// Result from async method here.
}];
I am uncertain how showAlertViewForRequestOperationWithErrorOnCompletion from the new AFNetworking is supposed to be used. I tried the following, but no alertView is shown.
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id requestObject) {
// ...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[UIAlertView showAlertViewForRequestOperationWithErrorOnCompletion:operation
delegate:nil];
}];
Are you sure that that line of code is being reached?
An alert is only shown if the error of the operation is non-nil. Are you sure this is the case?
Update:
Ok, so it looks like by the implementation that calling this doesn't show the alert right away, but just sets it up so that when the operation fails, an alert is showed. So you'd have to probably do:
[UIAlertView showAlertViewForRequestOperationWithErrorOnCompletion:requestOperation
delegate:nil];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id requestObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];