Change the possibility of failure

An achievable Apple developer example causes a memory leak when Wi-Fi is turned off while playing music offline ( using this code ) in my application, and it stuck the line that I mentioned below can be caused by this player code, since the log He speaks[__NSCFString reachabilityDidChange:]: unrecognized selector

[[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:noteObject];

in this method:

static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
{
#pragma unused (target, flags)
    NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
    NSCAssert([(__bridge NSObject*) info isKindOfClass: [CDVReachability class]], @"info was wrong class in ReachabilityCallback");

    CDVReachability* noteObject = (__bridge CDVReachability *)info;
    // Post a notification to notify the client that the network reachability changed.
    [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotificationString object: noteObject];
}

Here is the log:

2015-04-12 19:52:03.416 HelloCordova[4094:1250289] -[__NSCFString reachabilityDidChange:]: unrecognized selector sent to instance 0x174232820

And reachabilityDidChangeis in this file

#import "ReachabilityManager.h"
#import "Reachability.h"

@interface ReachabilityManager ()
@property NetworkStatus previousReachability;
@end

@implementation ReachabilityManager

- (id) init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                selector:@selector(reachabilityDidChange:)
                                                name:kReachabilityChangedNotification
                                                object:nil];
        self.previousReachability = -1;
    }
    return self;
}

- (void) reachabilityDidChange:(NSNotification *)notification {
    NSLog(@"reachabilityDidChange!");

    CDVReachability * r = [notification object];
    NetworkStatus ns = [r currentReachabilityStatus];

    if (self.delegate && [self.delegate respondsToSelector:@selector(reachabilityDidChangeFrom:to:)]) {
        [self.delegate reachabilityDidChangeFrom:self.previousReachability to:ns];
    }
    self.previousReachability = ns;
}

@end

I don't know, this is correct, but I try this:

dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:noteObject];
    });}

and even this:

// We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively
    // in case someon uses the Reachability object in a different thread.
    @autoreleasepool {
        Reachability* noteObject = (__bridge Reachability*)info;
        // Post a notification to notify the client that the network reachability changed.
        [[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:noteObject];
    }
+4
source share

All Articles