UIViewController is unloaded from the view stack, and NSURLConnection throws an error message

I click UIViewControlleron UINavigationController. This view controller immediately starts loading the XML feed and then parses it. However, if you press the back button before it is downloaded and it fails with EXC_BAD_ACCESS. The line that breaks into it is in parserDidEndDocumentand is the following line:

if (self.delegate && [self.delegate conformsToProtocol:@protocol(ModelDelegate)]) [self.delegate modelDidFinishParsing:self];

I assume it crashes because it is trying to access self.delegate, which is no longer assigned. How do I get around this?

In addition, I would issue a model object in a method modelDidFinishParsing. How would I release this model if it never reaches this method.

+3
source share
2 answers

I set objects to handle my downloads (and other asynchronous or long tasks) in AppDelegate, and then run them as needed from different controllers. Thus, they belong and have constancy throughout the life of the application.

The best way to do this is to pass them to the viewControllers that they will need (and not the viewController, expecting appDelegate to have such and such an object, ready and waiting) - dependency injection.

- , , , NSNotifications, , . , , viewWillDisappear .., , .

+1

, , , , NSURLConnection ( ), , , .

.. , self.delegate, , , ( ).

- (nil) self.delegate , UIViewController .

. Cocoa. , , (. NSURLConnection).

0

Source: https://habr.com/ru/post/1746772/


All Articles