开发者

Creating a new NSURLConnection inside of connectionDidFinishLoading

开发者 https://www.devze.com 2023-03-06 04:01 出处:网络
I have a NSURLConnection that gets data from a JSON web service, and everything works fine. I\'m using it to post something to the server and get a success response.

I have a NSURLConnection that gets data from a JSON web service, and everything works fine. I'm using it to post something to the server and get a success response.

After that call I want to initiate another NSURLConnection to refresh the data, so I'm doing so inside the connectionDidFinishLoading method, however this second connection isn't calling connectionDidFinishLoading when it is done loading.

Can I not initiate a NSURLConnection from inside the connectionDidFinishLoading method?

EDIT: Below is the code. I subclassed NSURLConnection to include a Tag NSString, calling the new class NSURLConnectionHelper. I'm using this to differentiate which connection has called the connectionDidFinishLoading.

- (void)connectionDidFinishLoading:(NSURLConnectionHelper *)connection
{ 

        if([connection.Tag isEqual:@"NewMessage"]){

            NSString *jsonString = [[NSString alloc] initWithData:receivedNewMessageData encoding:NSASCIIStringEncoding];

            NSDictionary *results = [jsonString JSONValue];

            [jsonString release];

            [connection release];

            if ([[results objectForKey:@"MessageAdded"] isEqual:@"True"]) {

                User *newUser = [[User alloc] init];
                [newUser retrieveFromUserDefaults];

                if([newUse开发者_如何学编程r IsLoggedIn]){
                Message *message = (Message *)[messages objectAtIndex: 0];

                        NSString *urlAsString = // url for webservice goes here

                        NSURL *url = [NSURL URLWithString:urlAsString];
                        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
                        NSURLConnectionHelper *connection1 = [[NSURLConnectionHelper alloc] initWithRequest:request delegate:self];
                        connection1.Tag = @"GetLatestMessages";

                        [request release];

                        if (connection1) {
                                receivedLatestMessagesData = [[NSMutableData data] retain];
                        } else {
                                // Inform the user that the connection failed.
                        }
                }            
            }
      }else if([connection.Tag isEqual:@"GetLatestMessages"]){
    //do some other stuff but this code is never reached
  }
}


I'm not familiar with NSURLConnectionHelper but it looks like you're never starting the connection.


I ended up having a space in my web service url, once I corrected that it worked.

0

精彩评论

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