NSURLSession Memory Leaks occur when using web services in IOS

前端 未结 4 914
面向向阳花
面向向阳花 2021-01-31 18:54

I am building an app that uses a web service and to get information from that web service I use NSURLSession and NSURLSessionDataTask.

I am now

4条回答
  •  后悔当初
    2021-01-31 19:15

    After rereading the URL Loading System Programming Guide it turns that I was setting the NSURLSession property to nil too early.

    Instead, I need to set the NSURLSession property to nil AFTER I receive the delegate message URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error, which makes sense. Luckily, it was a minor mistake.

    E.g.

    - (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error
    {
        if (error)
        {
    
    #ifdef DEBUG
            NSLog(@"[GPPhotoRequest] Session Invalidation: %@", [error description]);
    #endif
    
        }
    
        if ([session isEqual:_session])
        {
            [self cleanupSession];
        }
    }
    

提交回复
热议问题