implement Asynchronous method using NSURLConnection [closed] - iphone

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
MY Problem is that i connected connection with the delegate but it is not interrupting its delegate method. I want to fetch address for my annotation and display it in annotation view. i successfully fetched the address but the mapview loading takes time, that is why i used the NsConnection Method . Please help me if i have made any mistake.
CLLocationCoordinate2D coordinate;
coordinate.latitude = 28.6667;
coordinate.longitude = 77.2167;
clusterMap.region = MKCoordinateRegionMakeWithDistance(coordinate, 5000, 5000);
self.blocks = 4;
self.minimumClusterLevel = 100000;
// super.delegate = self;
zoomLevel = clusterMap.visibleMapRect.size.width * clusterMap.visibleMapRect.size.height;
NSMutableArray *pins = [[NSMutableArray alloc]init];
for(int i =0 ; i < 50; i++)
{
CGFloat latDelta = rand()*0.125/RAND_MAX - 0.02;
CGFloat lonDelta = rand()*0.130/RAND_MAX - 0.08;
newCoord.latitude = coordinate.latitude+latDelta;
newCoord.longitude = coordinate.longitude+lonDelta;
NSString *urlString = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",newCoord.latitude, newCoord.longitude];
// NSLog(#"Is%# main thread", ([NSThread isMainThread] ? #"" : #" NOT"));
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection start];
customannView = [[AnnotationView alloc]initWithLatitude:newCoord.latitude andLongitude:newCoord.longitude];
// customannView.title = [NSString stringWithFormat:#"Pin %i",i];
customannView.title =locationString;
customannView.subtitle = [NSString stringWithFormat:#"Pin %i",i];
customannView.coordinate = newCoord;
customannView.imgName = #"bookmark1.png";
[pins addObject:customannView];
[connection cancel];
}
[self addAnnotations:pins];
UILongPressGestureRecognizer *dropPin = [[UILongPressGestureRecognizer alloc] init];
[dropPin addTarget:self action:#selector(handleLongPress:)];
dropPin.minimumPressDuration = 0.5;
[clusterMap addGestureRecognizer:dropPin];
}
// [self performSelector:#selector(mapViewWillStartLoadingMap:) withObject:nil afterDelay:0.01];
UILongPressGestureRecognizer *dropPin = [[UILongPressGestureRecognizer alloc] init];
[dropPin addTarget:self action:#selector(handleLongPress:)];
dropPin.minimumPressDuration = 0.5;
[clusterMap addGestureRecognizer:dropPin];
}
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
[responseData setLength:0];
NSLog(#"Response :: %#",responseData);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
}

why are you doing
[connection cancel];
because it cancel the process before hitting the delegates.Remove that line and the code will work

The difficulty with your approach is that you are spawning a number of NSURLConnections and try then to handle with one delegate.
In order to figure out which connection and response data is associated to which latitude/longitude and response data object in your delegate method you would need to obtain the original request from the connection and then parse the URL which contains that info. This is cumbersome at least.
I would suggest to create a class (or use a third party library) that encapsulates one NSURLConnection request, the response data and possibly other state info. Purposefully, this class has a completion block where it becomes easy to associate the latitude/longitude and response data from the call site with each completion handler of a finished connection.

Related

NSURLConnection doesn't receive data when creating many downloading objects in iOS5

I have been searching for this problem on the SOF for several days and I still have not found the solution (say the same problem) yet.
I'm making and app that downloads 5 images simultaneously in an URL list (each image is on a different server).
I have an ImageDownloader class subclasses NSOperation and implements the NSURLConnectionDataDelegate.
So that I can add an instance of ImageDownloader to an operationQueue in the ViewController and it will run in a separate thread under the operationQueue. The line that add the downloader to the operationQueue is here:
downloader = [[ImageDownloader alloc] init];
[downloader downloadImageWithURL:[controller.URList objectForKey:[NSString stringWithFormat:#"%d",downloadIndex]] queue:queue andTag:downloadIndex + 100]; //my custom initialize
downloader.delegate = self;
[queue addOperation:downloader]; //use the addOperation method
Everything works fine in iOS6 but messed up in iOS5 (5.0 on my test device and 5.1 on my SDK), it just doesn't receive any response nor data by performing the methods didReceiveResponse and didReceiveData at all (these 2 methods are not jumped in).
After the timeout was exceeded, the runloop jumps into didFailWithError method and the program stalls.
As I understand, this means the runloop still runs right?
I tried to print out the error and all I got is: The request timed out.
When I reduce the number of downloading instances to 2 then it runs, but not with >=3 downloading instances.
One more information is that my network connection does limit the number of connection. But it work fine in iOS6, why it just doesn't work on iOS5?
I can still load the web in the simulator while the app is downloading.
So what kind of problem is this and how can I get over this problem?
Thanks in advance.
*Update:* as there are many classes and the problem's not been clearly detected yet, I will share here the whole project. You can download it directly from here:
DownloadingImage
As I just found out, if you're using credentials there is a chance that the server will reject them randomly every once in a while. So if you have a check to make sure previousFailureCount == 0 then you will most likely have a bug.
I've just figured out where my problem is, but not really understand why.
In my ImageDownloader class, I set up a runloop with done and currentRunLoop variables.
In the main method, I have a while loop for forcing the currentRunLoop run.
As I remove those "runLoop" stuffs, the app runs smoothly on both iOS6 and iOS5.
So change the entire ImageDownloader.m with these lines then it works (I commented out some useless (say harmful) lines):
//
// ImageLoader.m
// DownloadImagesTableView
//
// Created by Viet Ta Quoc on 6/25/13.
// Copyright (c) 2013 Viet Ta Quoc. All rights reserved.
//
#import "ImageDownloader.h"
#implementation ImageDownloader
#synthesize downloadData,delegate,queue,done,customTag;
NSRunLoop *currentRunLoop;
-(void)downloadImageWithURL:(NSString *)imageUrl queue:(NSOperationQueue*)opQueue andTag:(int)tag{
self.customTag= tag;
self.queue = opQueue;
// self.done = NO;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imageUrl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[connection start];
// currentRunLoop = [NSRunLoop currentRunLoop];
NSLog(#"Start downloading image %d...",customTag);
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(#"Received response...");
downloadData=[[NSMutableData alloc] initWithLength:0];
expectedDataLength=[response expectedContentLength];
NSLog(#"Image %d size: %lld kb",customTag,[response expectedContentLength]/1024);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
float receivedLenght = [data length];
receivedDataLength=(receivedDataLength+receivedLenght);
float progress=(float)receivedDataLength/(float)expectedDataLength;
[delegate updateProgess:progress andIndex:[NSIndexPath indexPathForRow:customTag-100 inSection:0]];
[self.downloadData appendData:data];
// NSLog(#"Percentage of data received of tag %d: %f %%",self.customTag,progress*100);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
[delegate finishedDownloadingImage:downloadData andTag:customTag];
// done = YES;
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Warning" message:#"Network Connection Failed?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil, nil];
// NSLog(#"%#",[error debugDescription]);
NSLog(#"Connection failed! Error - %# %#",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
[alert show];
}
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
NSLog(#"Got here *(*&(**&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&");
}
-(void)main{
// do{
//// NSLog(#"Running....1");
// [currentRunLoop runUntilDate:[NSDate distantFuture]];
// // [currentRunLoop run];
// } while (!done);
// [currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
}
#end
Thank you guys for your supports.
==================================================================================
P/s: for anyone who interested in this problem, I update here my entire solution: DownloadImage_Final

unable to parse JSON data from a NSURLConnection response

I am getting a server response of the form:
results are:{
AverageMark = 40;
"Grade A" = 10;
"Grade B" = 20;
"Grade C" = 30;
"Grade D" = 20;
MaxMark = 99;
MinMark = 44;
ProfileGrade = "";
ProfileMark = 1;
}
However I am unable to save the response data into an Array.
This is my code inside didReceiveResponse:
{
NSString *jsonString = [[NSString alloc] initWithString:responseData];
NSArray *jsonResults = [jsonString JSONValue];
NSLog(#"results are:%#",jsonResults); //this log is shown above
for (int i=0; i<[jsonResults count]; i++)
{
NSDictionary *AllData=(NSDictionary *)[jsonResults objectAtIndex:i]; //Program is crashing here--//
NSMutableArray *DataArray=[[NSMutableArray alloc]init];
NSString *avgMarkString;
avgMarkString=(NSString *)[AllData objectForKey:#"MaxMark"];
[DataArray addObject:avgMarkString];
}
}
I want to save the response data into the array called "DataArray". But the program is crashing.
What am I doing wrong?
You likely don't have the complete data yet in -connection:didReceiveResponse:. Create an instance variable or property of the type NSMutableData and initialize the data ivar or property in
-connection:didReceiveResponse: if you get a valid statusCode (between 200-299 should be ok). Use appendData: on the data object in the -connection:didReceiveData: delegate method. Finally in -connectionDidFinishLoading: the data is complete and can be parsed into JSON.
Alternatively you could just use the AFNetworking library. The library got some convenience methods for dealing with XML, JSON, images, etc...
Read the following page to get an introduction into the capabilities of AFNetworking: http://engineering.gowalla.com/2011/10/24/afnetworking/
Some example code from one of my own projects for downloading using a queue using NSURLConnectionDelegate methods. The URL Request objects are a custom subclass of NSURLConnection for some block "callbacks":
#pragma mark - URL connection delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSRange range = NSMakeRange(200, 99);
if (NSLocationInRange(httpResponse.statusCode, range));
{
self.data = [[NSMutableData alloc] init];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// inform caller that download is complete, provide data ...
if (_request.completionHandler)
{
_request.completionHandler(_data, nil);
}
[self removeRequest:_request];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
DLog(#"%#", error);
// inform caller that download failed, provide error ...
if (_request.completionHandler)
{
_request.completionHandler(nil, error);
}
[self removeRequest:_request];
}
that isn't json, try having a look at this http://json.org/example.html
Given JSON response is invalidate. Validate your JSON response here.

iOS image resolution issue

In My project I am using image view which downloads image from server. It is working fine on iOS 4 but it is not showing on iOS 5.
Is there any minimum resolution needs to be take care while using iOS 5. One of image which comes from server is of 72 dpi resolution which works on iOS 4 but not on iOS 5.
I have written category to image view which will download code from image URL
Here is code snippet:
- (void) setImageFromServer:(NSString *) imageURL
{
if (imageURL!=nil)
{
ImageDownloader *imageDownloader = [[[ImageDownloader alloc] init] autorelease];
imageDownloader.requester = self;
[imageDownloader startDownload:imageURL];
}
}
- (void) didDownloadImageData:(NSData *) data forImageURL:(NSString *) imageURL
{
[self setImage:[UIImage imageWithData:data]];
}
In downloader file :
- (void) startDownload:(NSString *)MyimageURL {
self.imageData = [NSMutableData data];
self.currentImageURL = MyimageURL;
self.downloadConnection = [NSURLConnection connectionWithRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:self.currentImageURL]]
delegate: self];
[self.downloadConnection start];
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[imageData appendData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[self.requester didDownloadImageData:self.imageData forImageURL:self.currentImageURL];
isRewardTagImageAvailable = YES;
[connection release];
connection = nil;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
}
I am not sure if this is a problem, but usually when I am writing a NSURLRequest I first initialize things as follows and the insert the request into a NSURLConnection. Sort of like this. Also note that once you initWithRequst in a NSURLConnection, you do not have to tell that connection to start, it will automatically.
NSURLRequest *tempReq = [[NSURLRequest alloc] initWithURL:someURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
NSURLConnection *tempCon = [[NSURLConnection alloc] initWithRequest:tempReq delegate:self];
I mean it should not matter, but give that a shot because looking at your, code it looks fine.
I would recommend adding the didFailWithError method:
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
To your files as well because maybe your connection is failing for some reason as well.
Is your connection synchronous or async ?
If async, you shouldn't autorelease your ImageDownloader, because there are some chance that it would be released for the connectionDidFinishLoading message.
Try to alloc your ImageDownloader normally, then release it when it finishes the download (both in connectionDidFinishLoading and didFailWithError)

Multiple NSURLConnection & NSRunLoop

I am trying to speed up my application download speed. I used Asynchronous NSURLConnection to download contents from the server, it was working fine with one connection.
I use the code from this post to implement multiple delegate objects. Multiple NSURLConnection delegates in Objective-C
When I created 2 NSURLConnection objects, each one is trying to download different files.
The callback didReceiveData routine was called but the it only received data of the first NSURLConnection object until the first connection was done then it started to receive the data from the second NSURLConnection. I want these two connections to receive data at the same time,what should I do? Here is my current code.
-(IBAction) startDownloadClicked :(id) sender
{
while (bDownloading)
{
int nCurrentCon = 0;
while (nCurrentCon < 2)
{
[self downloadAFile:[filenameArray objectAtIndex:nCurrentCon]];
nCurrentCon++;
}
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
}
}
- (void) downloadAFile: (NSString*) filename
{
NSString* urlstr = #"ftp://myftpusername:password#hostname";
NSURLRequest* myreq = [NSURLRequest requestWithURL:[NSURL URLWithString:urlstr]];
DownloadDelegate* dd = [[DownloadDelegate alloc] init]; //create delegate object
MyURLConnection* myConnection = [[MyURLConnection alloc] initWithRequest:myreq delegate:dd
startImmediately:YES];
}
Then in my Delegate Object, I implemented these routines
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receiveBuffer setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(#"receiving data for %#", targetFileName); //the file name were set when this delegate object is initialized.
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"Download Failed with Error - %# %#",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"File %# - downloaded.", targetFileName);
}
Your code looks okay. I have a similar setup that works successfully (although there seems to be a limit of four concurrent conections).
The main difference between your and my code is that you use FTP while I use HTTP. Why don't you try it with HTTP connections just to see whether you have run into a restriction of FTP connections on the iPhone?

iPhone app crashes with no justifiable reason?

I am developing an app in which I have a table. In the table cell I have an imageview ( the images are displayed via url ) and a textview/webview. I start threads for each row to get the images in the
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method ( if image is not already got ) and set the text of textview/webview from an array.
The problem arises when the images are being received and I pop the view, the application crashes giving the following message:
bool _WebTryThreadLock(bool), 0x1a0670: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
Now the situation becomes more weird if I don't release the textview/webview that I added to the cell, then every thing works fine.
Edit: the crash does not happens when I replace textview/webview with a label
Hoping I am clear in my question. If any thing is confusing please comment. I need to resolve this.
Thanks,
Nikhil
That's a huge mistake to use threads. Try to avoid using threads if you have other solutions !
In your case, just use an asynchronous NSURLConnection which will take care of downloading your image while not slowing your app ;)
Here is part of the code :
- (void) startDownload {
self.activeDownload = [NSMutableData data];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:#"blablabla"]] delegate:self];
self.imageConnection = conn;
[conn release];
}
#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.activeDownload appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"ERROR DOWNLOADING");
// Clear the activeDownload property to allow later attempts
self.activeDownload = nil;
// Release the connection now that it's finished
self.imageConnection = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"FINISH DOWNLOAD");
UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
self.activeDownload = nil;
self.imageConnection = nil;
//do whatever you want with your image
[image release];
}