NSURLConnection delegation and threading - iPhone

前端 未结 2 671
有刺的猬
有刺的猬 2020-12-16 07:56

I have a class that updates two .plist files in the app documents directory via an NSURLConnection. The class acts as its own delegate for NSURLConnection. It works proper

相关标签:
2条回答
  • 2020-12-16 08:15

    I found something interesting with NSURLConnection and NSThread - the thread will only live as long as it takes to perform the method that you call from it.

    In the case above the thread will live only as long as getNewDatabase:(NSString *)dbName takes to complete, therefore killing off any of its delegate methods before they actually have time to do anything.

    I found this website that gives a better explanation and a solution to the problem

    I tweaked it a little bit so I could have a custom time out if it didn't complete in a given time frame (handy when someone is walking around between access points)

        start = [NSDate dateWithTimeIntervalSinceNow:3];
    
        while(!isFinished && [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 
                                                      beforeDate:[NSDate distantFuture]]){
    
        if([start compare:[NSDate date]] == NSOrderedAscending){
            isFinished = YES;
        }
    }
    
    0 讨论(0)
  • 2020-12-16 08:31

    As it stands currently in the code you provided, getNewDatabase: is running on the main thread of your application. The problem in this particular case then is something other than the life cycle of the thread, as James observed in his case.

    If you did intend to perform this operation in the background, I'd recommend looking into using NSOperationQueue and NSOperation rather than solving the problem with the current code. I think your case is a great fit for NSOperationQueue, especially given that you have more than one download task to perform.

    Dave Dribin has an excellent article about using asynchronous API, such as NSURLConnection, inside an NSOperation. Alternatively, as long as you're running in a background thread, you can also simplify the process and just use a synchronous API method instead in your NSOperation, such as initWithContentsOfURL:.

    Marcus Zarra has also written a tutorial that demonstrates how easy it is to incorporate and use NSOperationQueue for simple background operations.

    0 讨论(0)
提交回复
热议问题