So this is my first post, I've found this site incredibly informative in my brief history with Objective C and iOS programming. Anyhow, I've run into a problem of sorts. A quick summary: I'm attempting to write a login form, which uses calls a custom class that with hit a webserver to auth using NSURLConnection. I'm using protocols and delegates to delegate back to the calling class to perform a segue to the main menu view controller once the authentication is complete.
The problem is that the menu I'm attempting to segue into takes anywhere from 6 to 75 seconds to display. If I remove the API call, it loads immediately. However, I'm doing logging throughout the process, and everything appears to step through at a normal pace. I even log when the menu view controller is loaded, and all logging happens normally. But the actual display of the menu is delayed!
Here are some code details:
View Controller Methods:
- (void) userLogin:(NSString *)userName password:(NSString *)password {
NSLog(#"VC login method");
api = [theAPI getSelf];
[api setDelegate:self];
[api userLogin:userName password:password];
}
- (void) userLoginDone:(BOOL)successful {
[self performSegueWithIdentifier:#"sgLoginToMainMenu" sender:self];
NSLog(#"Login Done");
}
API Method:
- (void) userLogin:(NSString *)userName password:(NSString *)password {
NSURL *url = [NSURL URLWithString:(NSString *) [API_PATH stringByAppendingString:#"test.html"]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSDictionary *json = [[JSON new] parseJSON:data];
self.usrID = [json objectForKey:#"usrID"];
self.sessionID = [json objectForKey:#"sessionID"];
self.userName = [json objectForKey:#"Username"];
NSLog(#"Username: %#", [json objectForKey:#"Username"]);
[[self delegate] userLoginDone:YES];
}];
}
All the NSLogs execute in a normal timespan (few milliseconds). Yet the main menu view controller takes entirely too long to appear! I'm very new to iOS programming, so I'm hoping I'm just overlooking something that googling couldn't solve. Any help would be greatly appreciated!
You need to update the UI on the main thread, but userLoginDone: is being called on an NSOperationQueue, which create its own separate thread. This could explain the delay in displaying. Have you tried using [NSOperationQueue mainQueue] (which returns the queue associated with the main thread) to pass to sendAsynchronousRequest: instead?
Related
I've read through tons of messages saying the same thing all over again : when you use a NSURLConnection, delegate methods are not called. I understand that Apple's doc are incomplete and reference deprecated methods, which is a shame, but I can't seem to find a solution.
Code for the request is there :
// Create request
NSURL *urlObj = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlObj cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
[request setValue:#"gzip" forHTTPHeaderField:#"Accept-Encoding"];
if (![NSURLConnection canHandleRequest:request]) {
NSLog(#"Can't handle request...");
return;
}
// Start connection
dispatch_async(dispatch_get_main_queue(), ^{
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; // Edited
});
...and code for the delegate methods is here :
- (void) connection:(NSURLConnection *)_connection didReceiveResponse:(NSURLResponse *)response {
NSLog(#"Receiving response: %#, status %d", [(NSHTTPURLResponse*)response allHeaderFields], [(NSHTTPURLResponse*) response statusCode]);
self.data = [NSMutableData data];
}
- (void) connection:(NSURLConnection *)_connection didFailWithError:(NSError *)error {
NSLog(#"Connection failed: %#", error);
[self _finish];
}
- (void) connection:(NSURLConnection *)_connection didReceiveData:(NSData *)_data {
[data appendData:_data];
}
- (void)connectionDidFinishDownloading:(NSURLConnection *)_connection destinationURL:(NSURL *) destinationURL {
NSLog(#"Connection done!");
[self _finish];
}
There's not a lot of error checking here, but I've made sure of a few things :
Whatever happens, didReceiveData is never called, so I don't get any data
...but the data is transfered (I checked using tcpdump)
...and the other methods are called successfully.
If I use the NSURLConnectionDownloadDelegate instead of NSURLConnectionDataDelegate, everything works but I can't get a hold on the downloaded file (this is a known bug)
The request is not deallocated before completion by bad memory management
Nothing changes if I use a standard HTML page somewhere on the internet as my URL
The request is kicked off from the main queue
I don't want to use a third-party library, as, ultimately, these requests are to be included in a library of my own, and I'd like to minimize the dependencies. If I have to, I'll use CFNetwork directly, but it will be a huge pain in the you-know-what.
If you have any idea, it would help greatly. Thanks!
I ran into the same problem. Very annoying, but it seems that if you implement this method:
- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL
Then connection:didReceiveData: will never be called. You have to use connectionDidFinishLoading: instead... Yes, the docs say it is deprecated, but I think thats only because this method moved from NSURLConnectionDelegate into NSURLConnectionDataDelegate.
I like to use the sendAsynchronousRequest method.. there's less information during the connection, but the code is a lot cleaner.
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if (data){
//do something with data
}
else if (error)
NSLog(#"%#",error);
}];
From Apple:
By default, a connection is scheduled on the current thread in the
default mode when it is created. If you create a connection with the
initWithRequest:delegate:startImmediately: method and provide NO for
the startImmediately parameter, you can schedule the connection on a
different run loop or mode before starting it with the start method.
You can schedule a connection on multiple run loops and modes, or on
the same run loop in multiple modes.
Unless there is a reason to explicitly run it in [NSRunLoop currentRunLoop],
you can remove these two lines:
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[connection start];
or change the mode to NSDefaultRunLoopMode
NSURLConnection API says " ..delegate methods are called on the thread that started the asynchronous load operation for the associated NSURLConnection object."
Because dispatch_async will start new thread, and NSURLConnection will not pass to that other threat the call backs, so do not use dispatch_async with NSURLConnection.
You do not have to afraid about frozen user interface, NSURLConnection providing only the controls of asynchronous loads.
If you have more files to download, you can start some of connection in first turn, and later they finished, in the connectionDidFinishLoading: method you can start new connections.
int i=0;
for (RetrieveOneDocument *doc in self.documents) {
if (i<5) {
[[NSURLConnection alloc] initWithRequest:request delegate:self];
i++;
}
}
..
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
ii++;
if(ii == 5) {
[[NSURLConnection alloc] initWithRequest:request delegate:self];
ii=0;
}
}
One possible reason is that the outgoing NSURLRequest has been setup to have a -HTTPMethod of HEAD. Quite hard to do that by accident though!
The following are methods that I am using to retrieve data from a server while displaying a UIActivityIndicator. I'm trying to put these methods in the app delegate and then call them from other classes, but I don't know how to return my JSONData. Can anybody help with this?
-(void)startProcess:(NSString *)buildURL{
UIActivityIndicatorView *aInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActionSheetStyleBlackTranslucent];
[aInd setFrame:CGRectMake(0, 0, 50, 50)];
[aInd startAnimating];
// then call the timeCOnsumingmethod in separate thread.
[NSThread detachNewThreadSelector:#selector(getData:) toTarget:self withObject:buildURL];
}
- (void)getData:(NSString *)buildURL{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Query our database for a restaurant's menus
NSURL *url = [NSURL URLWithString:buildURL];
NSError *e;
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&e];
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
// NSError *error = nil;
[self performSelectorOnMainThread:#selector(endProcess:) withObject:jsonData waitUntilDone:YES];
[pool release];
//return jsonData;
}
- (IBAction)endProcess:(NSData *)jsonData{
// ??????????
return jsonData;
}
Not sure why got downvoted but your approach is all wrong. Here's what you want to do:
Add the UIActivityIndicatorView
Use NSURLConnection to asynchronously retrieve the data
Use NSJSONSerialization to decode the received JSON into a NSDictionary or NSArray
Remove the UIActivityIndicatorView
Your best bet would be to implement this as a separate class that takes a delegate object. You could implement a delegate protocol to indicate states like 'started network activity' (which your delegate could use to add a spinner view), and 'received data' (which would pass the decoded object back to the delegate - the delegate could then remove the spinner).
One of the benefits of this approach is you can easily set it up so that the connection/request is canceled when the object deallocs. Then you just store the request object as a property on your delegate, and when your delegate goes away, it deallocs the request, which cancels/cleans up properly.
I need to make some POST calls to my server, but I need to not block the main thread. As I understand, NSMutableURLRequest and NSURLConnection are not thread safe, so it is best to use the async method of NSURLConnection.
My question about this is, how I can package it up nicely into a method, instead of having to use the delegate method? I would prefer to do:
NSData *returnedData = [Utility postDataToURL:#"some string of data"];
This is how it is easy done with the following method:
[NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];
It is so nice keeping everything within one method, then just having my data returned from it!
Are there any block based methods for this? It becomes an issue when I need to write methods for about 50 different calls and each one needs to use the same delegate method. Am I going about this the wrong way?
This will only need to be for iOS5.
iOS 5 adds sendAsynchronousRequest:queue:completionHandler: which does what I think you want. I've got my code set up to use that if available, but to fall back on performing a synchronous fetch on a background GCD queue and hopping onto the main thread with the result if it doesn't. The latter will be less power efficient but it's just to maintain legacy support.
if([NSURLConnection respondsToSelector:#selector(sendAsynchronousRequest:queue:completionHandler:)])
{
// we can use the iOS 5 path, so issue the asynchronous request
// and then just do whatever we want to do
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:
^(NSURLResponse *response, NSData *data, NSError *error)
{
[self didLoadData:data];
}];
}
else
{
// fine, we'll have to do a power inefficient iOS 4 implementation;
// hop onto the global dispatch queue...
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
// ... perform a blocking, synchronous URL retrieval ...
NSError *error = nil;
NSURLResponse *urlResponse = nil;
NSData *responseData =
[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
// ... and hop back onto the main queue to handle the result
dispatch_async(dispatch_get_main_queue(),
^{
[self didLoadData:responseData];
});
});
}
In production code you'd actually check the errors and HTTP response codes (as a server 404 response is probably just as much an error from your point of view as a connection failure), obviously.
iOS 5.0 > you can use sendAsynchronousRequest method look at NSURLConnection Class and it uses blocks. If you want to support iOS 4.0 > too then you have to write one of your own block based Asynchronous URL loading which is fairly easy to write. You are better off by using MKNetworkKit.
but I need to not block the main thread. As I understand, NSMutableURLRequest and NSURLConnection are not thread safe, so it is best to use the async method of NSURLConnection.
You don't want to do Synchronous network connection it blocks thread whichever it is called from (its even worse if its main thread). You can do Asynchronous network connection on main thread. If you want to do call NSURLConnection on non-main thread then have to create a RunLoop on that thread (if you don't then the delegate methods of NSURLConnection never gets called).
I had this problem pre-5.0 so I made a little class to handle the NSURLConnection delegate protocol and offer callers an interface with closures:
BetterNSURLConnection.h
#property (retain, nonatomic) NSURLRequest *request;
BetterNSURLConnection.m
#property (retain, nonatomic) NSURLConnection *connection;
#property (retain, nonatomic) NSHTTPURLResponse *response;
#property (retain, nonatomic) NSMutableData *responseData;
#property (copy, nonatomic) void (^completionBlock)(id, NSHTTPURLResponse *);
#property (copy, nonatomic) void (^errorBlock)(NSError *);
... You can add typedefs to make those block signatures prettier...then:
#synthesize connection = _connection;
#synthesize response = _response;
#synthesize responseData = _responseData;
#synthesize completionBlock = _completionBlock;
#synthesize errorBlock = _errorBlock;
#synthesize request=_request;
- (void)startWithCompletion:(void (^)(id, NSHTTPURLResponse *))completionBlock error:(void (^)(NSError *))errorBlock {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
self.completionBlock = completionBlock;
self.errorBlock = errorBlock;
self.responseData = [NSMutableData data];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:self.request delegate:self];
self.connection = connection;
[self.connection start];
[connection release];
}
... then do the delegate like this:
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSHTTPURLResponse *)response {
[self.responseData setLength:0];
self.response = response;
}
- (void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)data {
[self.responseData appendData:data];
}
- (void)connection:(NSURLConnection *)aConnection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
self.errorBlock(error);
self.connection = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (self.response.statusCode >= 400) {
self.errorBlock(error);
} else {
// i do json requests, and call a json parser here, but you might want to do something different
id result = [self parseResponse:self.responseData];
self.completionBlock(result, self.response);
}
self.connection = nil;
}
I use a facade method to op-queue an internal worker which issues the synchronous call. Depending on the rate at which you send the calls, it might work. Example:
// Presented in #interface
-(void)sendPostRequest {
// Last chance to update main thread/UI
NSInvocationOperation *op = [[[NSInvocationOperation alloc] initWithTarget:self selector:#selector(sendPostRequest_internal) object:nil] autorelease];
[opQueue addOperation:op];
}
// Hidden in #implementation
-(void)sendPostRequest_internal {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURLRequest *request = // yadda, you might use NSURLMutableRequest
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:response error:error];
// process data, retain things as needed, post results using performSelectorOnMainThread:
[pool release];
}
It works pretty well for my purposes but you might need to delve a little deeper in to the async stuff, which really isn't too bad.
I'm kind of a newbie in objective-c and I'm having issues when I call a NSArray from other class. I have a class which handles the parsing of a XML feed and another to manage the UItableview stuff. It's weird because when it's done synchronously (using NSXMLParser methods) all data is shown in the table, but when I use the NSURLConnection to make it asynchronous it parses all the data but the array is empty when it's called. If I call NSLog it shows all data contained the newsStories array when the data is being parsed, but somehow its deleted when I call it.
On the parser class I have and all the methods of the NSXMLParser:
- (void)parseXMLFileAtUrl:(NSString *)URL {
data = [[NSMutableData alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (connection) {
data = [[NSMutableData alloc]init];
}
[connection release];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//Reset the data as this could be fired if a redirect or other response occurs
[data setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data
{
//Append the received data each time this is called
[data appendData:_data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//Start the XML parser with the delegate pointing at the current object
_parser = [[NSXMLParser alloc] initWithData:data];
newsStories = [[NSMutableArray alloc] init];
[_parser setDelegate:self];
[_parser setShouldProcessNamespaces:NO];
[_parser setShouldReportNamespacePrefixes:NO];
[_parser setShouldResolveExternalEntities:NO];
[_parser parse];
}
And this is how I call the array:
-(BOOL) loadData{
NSString *latestUrl = [[NSString alloc] initWithString:feed];
if ([latestArray count] == 0) {
news = [[news_parser alloc]init];
[news parseXMLFileAtUrl:latestUrl];
[self setArray:news.newsStories];--- here it says null for Array and for newsItems
}
[latestUrl release];
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
array = [[NSMutableArray alloc]init];
_News.rowHeight =85;
[self loadData];
[self._News reloadData];
}
Any help would be appreciated, thanks guys!
Regards.
... Do you understand what asynchronous means? It means that your function will return and the connection will continue, making the callbacks when it is ready. The way you have this coded up, you start the connection and then immediately try to use the data -- it isn't there yet! You need to wait until after connectionDidFinishLoading before you try to use the array.
Do some more research on what exactly it means to be asynchronous; it seems you're not understanding that.
Edit
Let me clarify, since you seem to have missed my point. Your viewDidLoad function finishes long before your connectionDidFinishLoading callback gets called, and so of course the newsStories array is not there yet. When you call:
[news parseXMLFileAtUrl:latestUrl];
in the loadData function, that doesn't stop and wait for the connection to return; if it were synchronous, it would, but asynchronous does not. (Hence I ask you to research what asynchronous actually means, which apparently you still have not done). Since that call returns and then you immediately try to use the loaded data (long before the connectionDidFinishLoading is called) you naturally don't have any data in there.
From Apple's docs:
Mutable objects are generally not thread-safe. To use mutable objects
in a threaded application, the application must synchronize access to
them using locks. (For more information, see “Atomic Operations”). In
general, the collection classes (for example, NSMutableArray,
NSMutableDictionary) are not thread-safe when mutations are concerned.
That is, if one or more threads are changing the same array, problems
can occur. You must lock around spots where reads and writes occur to
assure thread safety.
Reading this might help you out. Not totally sure if that is what is going on in your app but it looks like a good place to start.
I am trying to download a file from a server. My code is following. In didFinishLaunchingWithOptions method, I create a new thread using detachNewThreadSelector which runs the following code.
NSString *destPath = [self.home_dir_path stringByAppendingPathComponent:[NSString stringWithFormat:#"_%#",content_data_file_name]];
[ContentBO downloadFile:destPath content_name:content_data_file_name];
if([self updatesAvailable]){
//update content
}else{
//launch app
}
My code for downloadFile is:
#try{
NSString *url = [NSString stringWithFormat:#"%#/%#",ServerURL,content_name];
NSLog(#"downloading URL is: %#",url);
self.request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];
[self.request setRequestMethod:#"GET"];
[self.request setDownloadDestinationPath:destFilePath];
NSLog(#"destination path is: %#",destFilePath);
[self.request setTimeOutSeconds:30];
[self.request setDelegate:self];
[self.request startSynchronous];
NSError *error = [self.request error];
NSData *receivedData = nil;
if (!error) {
isSuccess = YES;
self.responseStr = [request responseString];
receivedData = [NSData dataWithData:[self.request responseData]];
}
else {
isSuccess = NO;
NSLog(#"The following error occurred: %#", error);
}
}#catch(NSException *e){
NSLog(#"exception occured.");
}
What my understanding about synchronous call is that this is a blocking call and control should not go below
[ContentBO downloadFile:destPath content_name:content_data_file_name];
until control is out of requestFinished method of ASIHTTPRequestDelegate. In my case what happening is that the control is simultaneously executing code in requestFinished and below
[ContentBO downloadFile:destPath content_name:content_data_file_name];
But I don't want the control to go below [ContentBO downloadFile...] before coming out of requestFinished method.
The requestFinished delegate call is run on the main thread asynchronously, and your code is not running on the main thread, so it is expected that both would run at the same time.
However, as you are using synchronous requests why not remove the contents of requestFinished and put the code after the 'startSyncronous' line? You are guaranteed the request has finished when startSynchronous returns.
In one of my projects the app had to do heavy server side data syncing. In that process one operation should had start after the successful execution of it's previous process and I was using ASIHttp synchronous requests in that. I was facing the same issue you mentioned, so to tackle it I used NSCondiiton. All it requires that you lock the thread after you call:
[self.request startSynchronous];. When the requests delegate method is called after the exection of the request, issue a signal command, and the next line of the code after the thread lock statement will be executed. Here is a rough example:
//declare a pointer to NSCondition in header file:
NSCondition *threadlock;
-(id) init
{
threadlock = [[NSCondition alloc] init]; //release it in dealloc
}
-(void)downLoadFile
{
[thread lock];
//your request code
[self.request setDidFinishSelector:#selector(downLoadFileRequestDone:)];
[self.request setDidFailSelector:#selector(downLoadFileRequestWentWrong:)];
[self.request startSynchronous];
[thread wait];
//the lines here will be executed after you issue a signal command in the request delegate method
[thread unlock];
}
-(void) downLoadFileRequestDone:(ASIHTTPRequest *)request
{
[thread lock];
//perform desire functionality
//when you are done call:
[thread signal];
[thread unlock];
}
It worked perfect for me... hope it will help.