“-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data” not called

后端 未结 4 1392
清歌不尽
清歌不尽 2021-01-07 01:32

Have a look to this code snippet:-

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{               
    [webData         


        
4条回答
  •  我在风中等你
    2021-01-07 02:07

    I suspect you are having a memory management issue. I could be mistaken on this, but I believe that even:

    NSURLConnection* connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
    

    won't work, because connection will be released at the end of the containing method, when connection goes out of scope. Make sure NSURLConnection *connection and NSMutableData *data are declared as member variables where ever you are doing this, and that you alloc and init them appropriately. My code usually looks like:

        NSURLRequest *request = [NSURLRequest requestWithURL:url
                                              cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                              timeoutInterval:30.0];
        // cancel any old connection
        if(connection) {
            [connection cancel];
            [connection release];
        }
        // create new connection and begin loading data
        connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        if(connection) {
            // if the connection was created correctly, release old data (if any), and alloc new
            [data release];
            data = [[NSMutableData data] retain];
        }
    

    Also, release the connection and data in dealloc. For good measure, release and set them to nil at the very end of didFailWithError and didFinishLoading:

    [connection release];
    connection = nil;
    [data release];
    data = nil;
    

    Good luck; I've done this a million times, let me know if you cannot get it working.

提交回复
热议问题