I have an app currently using AFNetworking 1.0 to sync data from a REST Web Service.
When the sync occurs currently by tapping a button on the UI it blocks the UI.
I'm upgrading the app to use AFNetworking 2.0.
How can I make the sync happen on a background thread so the UI does not stall?
Did you try this:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://gowalla.com/users/mattt.json"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"Name: %# %#", [JSON valueForKeyPath:#"first_name"], [JSON valueForKeyPath:#"last_name"]);
} failure:nil];
Check the following link
https://github.com/AFNetworking/AFNetworking/wiki/Introduction-to-AFNetworking
Related
I'm using AFImageRequestOperation to download hundreds of jpg from my server.
NSURLRequest *request = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:20];
AFImageRequestOperation *operation;
operation = [AFImageRequestOperation imageRequestOperationWithRequest:request
imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {}
];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:[[paths objectAtIndex:0] stringByAppendingPathComponent:picture] append:NO];
[downloadQueue addOperation:operation];
If I delete them all (removing all images in Documents Folder) and start download again, the first X (depends on how far I got during last download process) operations are processed immediately. It seems like the images downloaded from the previous process are stored (cached) somewhere. I also checked the Documents Folder for the simulator and the images are downloaded correctly. So how can I make sure the download process really starts from the beginning?
When you're creating the NSURLRequest, use this instead:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:theURL];
request.cachePolicy = NSURLRequestReloadRevalidatingCacheData;
This policy is described in the docs the following way: Specifies that the existing cache data may be used provided the origin source confirms its validity, otherwise the URL is loaded from the origin source.
You can check the cache policies of NSURLRequest here.
I'm using AFImageRequestOperation to download hundreds of jpg from my server.
NSURLRequest *request = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:20];
AFImageRequestOperation *operation;
operation = [AFImageRequestOperation imageRequestOperationWithRequest:request
imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {}
];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:[[paths objectAtIndex:0] stringByAppendingPathComponent:picture] append:NO];
[downloadQueue addOperation:operation];
If I now want to cancel the download in progress I execute [downloadQueue cancelAllOperations].
With the previous version of AFNetworking that I used (earlier this year) this worked perfectly but with the recent one I get this:
ERROR [http://myImageURL] -- The operation couldn’t be completed. (NSURLErrorDomain error -999.)
for all pending operations.
Do I have to to do some additional stuff now?
In NSURLErrorDomain, that error code is defined as follows:
kCFURLErrorCancelled = -999
...which makes sense, since the operation was indeed cancelled. This is not a bug, but an expected behavior. The change may be either a documented change to AFNetworking, or an undocumented one in NSURLConnection between iOS versions.
I was wondering how i would get AFNetworking code to visit a link on my web server(PHP Script) and get the response data, and put it into a string?
Could anyone post an example of this?
Thanks alot!
here is the required code to get response from server using AFNETWorking.Just add AFNetworking Library and the Required frameWorks.After that use the below code.
NSURL *url = [[NSURL alloc] initWithString:#"http://itunes.apple.com/search?term=harry&country=us&entity=movie"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"JSON");
self.movies = [JSON objectForKey:#"results"];
[self.tbleView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Request Failed with Error: %#, %#", error, error.userInfo);
}];
self.movies is name of mutable Array you can use any name instead of this.
Also you can check here
https://github.com/AFNetworking/AFNetworking
AFHTTPRequests get passed default NSURLRequest objects. stock ones - no modification for AFNetwork
to build THAT see
How to add GET parameters to an ASIHttpRequest?
I'm working on an app where I want to pull data from a remote web service and populate a UITableView. When I get new data I want the currently visible cells to remain and add the new data above it, much like most Twitter clients does. My load method currently looks like this:
- (void)loadPostsInBackground
{
NSURL *url = [NSURL URLWithString:#"[URL]"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation;
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id jsonObject) {
[self createPostsFromDict:jsonObject];
[self.refreshControl endRefreshing];
[self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id jsonObject) {
NSLog(#"Received an HTTP %d", response.statusCode);
NSLog(#"The error was: %#", error);
}];
[operation start];
}
This works, but it updates the currently visible cells with the new data. So what I want is, get new data, add it above the currently visible cells (or stay at the currently visible cells). What is the best way to do this?
Note: I will require iOS6.
Instead of calling reloadData on the table view. You should call insertRowsAtIndexPaths:withRowAnimation:. Do that after updating the data used by the table view's data source.
I want to use AFNetworking http client synchronously. I am not sure how to go about this. The reason i choose to do it this way is because i am making a server call on the click of a button. I dont want the user to interact with the UI until I am done processing the response. Sample code would be extremely helpful.
Don't. Just Don't.
In the user side it would freez everything and make your app feel buggy.
At least you can use asynchronous request and a loader like that : https://github.com/samvermette/SVProgressHUD
Exemple :
[SVProgressHUD show];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://gowalla.com/users/mattt.json"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[SVProgressHUD dismiss];
} failure:nil];