Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Having a problem with NSURLConnection, if I create a NSURLConnection and call [connection connectionWithRequest] let it load a little then call [connection cancel] most of the time that works fine. However occasionally even after I call [connection cancel] the connection's delegate still gets called (which crashes the app). Googling around it looks like the problem here is a race condition in the runloop, I cancel the connection and release the delegate but before the runloop cycles it calls the delegate functions -> crash.

Is there a way for me to, after I call [connection cancel] confirm the connection has actually canceled? Even a crappy while() loop will do :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
754 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should not release the connection & associated storage until your delegate receives either a connectionDidFinishLoading: or a connectionDidFailWithError: message.

Delegates are not normally retained by the object they're acting as delegate for. However in this case it is, so the delegate should not become invalid while the NSURLConnection is still referring to it, unless you're over-releasing it somehow.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...