NSURLConnection didReceiveData not called

前端 未结 4 1840
自闭症患者
自闭症患者 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 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

提交回复
热议问题