开发者

Why NSURLConnection blocks my UI?

I've been reading several threads and questions about this issue but I didn't find the solution.

I have some asynchronous calls performed with

[NSURLConnection connectionWithRequest:anURLRequest delegate:self];

The problem is that I want the interface to be operative but it is blocked until the connection is finished.

Is this solved launching another thread? Where is exactly the problem?

EDIT

Ok, after retrieve data I parse it with NSXMLParser, that do it synchronously and blocks main thread. Is this correct? Then, maybe I need to parse in another thread开发者_如何学C. Anyone has a guide?


From the docs:

Messages to the delegate will be sent on the thread that calls this method. For the connection to work correctly the calling thread’s run loop must be operating in the default run loop mode.

Are you sure that this code is being called on a run loop in default mode and not from a thread created by yourself with a different type of run loop mode?


The UI should not be locking up when you use connectionWithRequest. Try creating a label in your UI and have your connection update it with the current amount of data, like so:

- (void)downloadContentFromUrl:(NSURL *)url {   
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (connection) {
        receivedData = [[NSMutableData data] retain];
        self.downloadProgressLabel.text = @"Downloading...";
    } else {
        // oh noes!
    }
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [receivedData setLength:0]; 
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {   
    [receivedData appendData:data]; 
    int kb = [receivedData length] / 1024;
    self.downloadProgressLabel.text = [NSString stringWithFormat:@"Downloaded\n%d kB", kb];
}

connectionWithRequest does indeed run in it's own thread - no need for you to worry about this. In fact it must be started from the main thread. Check out the NSUrlConnection doc for more info.


+ (id)connectionWithRequest:(NSURLRequest *)request delegate:(id)delegate];

This method should create an asynchronous-request (that means that it runs in the background and it doesn't block the UI). You should check if there's another class/method in your file that blocks the UI (for example NSData's '+ (NSData *)dataWithContentsOfURL:(NSURL *)URL').


I don't know if it could help anyone, but I've the same problem (asynchronous URL request blocking the UI) but it was due to:

NSLog(@"dataReceived: %@", data);

in the connectionDidReceiveData method.


In my case I was trying to update the UIProgressView.progress property. I calculated the new value like that

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.progress setProgress:self.downloadedData.length / self.fileSize ];
    [self.downloadedData appendData:data];
}

Which doesn't work, I replaced this snippet of code with

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    self.downloadedSize += data.length;
    [self.progress setProgress:self.downloadedSize / self.fileSize ];
    [self.downloadedData appendData:data];
}

And now the progress view updates with no problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜