How to avoid access to unoccupied objects in / etc callbacks?

The question was discussed here and here , but I wonder if there is a more reliable way to solve this, whether you have delegates or not - when the function is called after a delay. At a certain point in the program, when you click the button, an object is created - CCLayer. This layer creates several objects, some of them in callbacks. This created object layer has a back button that destroys it. I ran into a problem when calling calls, etc. Run AFTER this object is destroyed, and try to access objects that no longer exist - where "the message sent to the freed instance 0x258ba480" gives me this good news. How to avoid this?

1) Is there a way to kill callbacks (because I obviously don’t need them anymore) 2) should / I can check for the presence of these possibly nonexistent objects in the callbacks themselves 3) something else?

(My callback is the code for checking the Internet connection that I copied from this illustrious website - can it live long and flourish using Reachability, and I could solve the problem by simply moving it to the main screen and installing flag on child view but i don't want to.)

- (void)testInternetConnection
{
    internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(Reachability*reach)
    {
         // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Yayyy, we have the interwebs!");
            //I do the net stuff here
    });
};

// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
    // Update the UI on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Someone broke the internet :(");
        noNetMessageLabel.visible=true; //<-------this goes kaboom
        noNetFlag=true;

    });
};

[internetReachableFoo startNotifier];

}

+4
source share
2 answers

There are basically two ways to avoid delegate messaging:

  • , . , . - - , , . , - .

  • . , , nil, . . .

+5

:

:

if (noNetMessageLabel)
  noNetMessageLabel.visible = true;

.

, , - . AppDelegate :

- (NSError*)presentConnectivityAlert
{
   if () //any error condition checking appropriate
       [[NSNotificationCenter defaultCenter]
          postNotificationName:@"connectivityAlert"
          object:self
          userInfo:userInfo];
}

.

ViewControllers .

- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] 
      addObserver:self
      selector:@selector(didReceiveRemoteNotification:)                                                  
      name:@"connectivityAlert"
      object:nil];
}

- (void)viewDidUnload {
[[NSNotificationCenter defaultCenter] 
  removeObserver:self
  name:@"connectivityAlert"
  object:nil];
}

-(void)didReceiveRemoteNotification:(NSDictionary *)userInfo {
   if (self.isViewLoaded && self.view.window) {
      //Present the user with alert
   }
}

, .

( ), NSOperation NSOperationQueue. Reachability.

+3

All Articles