Вопрос

In my app, after some asynch operation I'm calling the selector pointing to the method in my ViewController. Sometimes it happens, that this ViewController is already deallocated after that asynch operation. My question is how can I check whether the ViewController is already deallocated? I have access to it as an Id variable. Simple checking whether it's nil doesn't work. This is the proper value of the ViewController:

enter image description here

but this one causes crash:

enter image description here How can I check if it's empty or sth like that? I'm new to ObjectiveC, so it might look like a layman question, but please help. Thanks for any answers.

Это было полезно?

Решение 2

My question is how can I check whether the ViewController is already deallocated?

The answer is that you can't (however, see below). When the object is deallocated, all that happens is that it's storage goes back on the heap to be reused. Nothing happens to the content of that memory. So your id pointer can either be pointing at the original non deallocated object, the original object but deallocated, a new object that has reused the storage or just garbage.

The old way around this is for the view controller (the delegate) to own the object doing the delegating (whatever it is doing the asynchronous operation). The view controller would then nil the delegate property (which would be an assign property) on the object doing the delegating when the view controller gets deallocated.

With ARC, it's the same arrangement except the delegate property on the object doing the delegating is a weak property which means that ARC will automatically nil it when the delegate gets deallocated.

Другие советы

You may want to investigate using weak properties - these are supposed to nil themselves out when the object being referenced gets deallocated. The answer to this question should be useful:

Differences between strong and weak in Objective-C

Try using DELEGATE or completion block

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top