可达性变化导致崩溃

Dev*_*pia 5 memory-leaks reachability nsnotificationcenter ios

苹果开发人员在我的应用程序中播放离线音乐(使用此代码)期间关闭 wifi 时,可达性导致内存泄漏的示例,并且卡住了我在底部提到的行,这可能是由播放器代码引起的,因为日志显示[__NSCFString reachabilityDidChange:]: unrecognized selector

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

在这个方法中:

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];
}
Run Code Online (Sandbox Code Playgroud)

这是日志:

2015-04-12 19:52:03.416 HelloCordova[4094:1250289] -[__NSCFString reachabilityDidChange:]: unrecognized selector sent to instance 0x174232820
Run Code Online (Sandbox Code Playgroud)

并且reachabilityDidChange在这个文件中

#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
Run Code Online (Sandbox Code Playgroud)

我不知道这是正确的,但我尝试这样做:

dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:noteObject];
    });}
Run Code Online (Sandbox Code Playgroud)

甚至这个:

// 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];
    }
Run Code Online (Sandbox Code Playgroud)