NSURLConnection didReceiveData not called

前端 未结 4 1839
自闭症患者
自闭症患者 2020-12-10 17:38

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\

相关标签:
4条回答
  • 2020-12-10 17:55

    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!

    0 讨论(0)
  • 2020-12-10 18:00

    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.

    0 讨论(0)
  • 2020-12-10 18:07

    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

    0 讨论(0)
  • 2020-12-10 18:17

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题