ios - 为什么 UINavigationController 无法使用 ARC 获取 block 回调参数的所有权

标签 ios objective-c automatic-ref-counting objective-c-blocks exc-bad-access

给出以下代码示例(iOS 7、Xcode 5):

/**
* SampleProvider Class
*/

typedef void(^RequestCallback)(UIViewController *result);

static NSString * const cControllerRequestNotification = @"controllerRequestNotification";
static NSString * const cRequestClassNameKey = @"className";
static NSString * const cRequestCallbackKey = @"callback";

@interface SampleProvider : NSObject
+ (void)requestControllerForClassName:(NSString *)className completion:(RequestCallback)callback;
@end

@interface SampleProvider ()
- (UIViewController *)controllerForClassName:(NSString *)className;
- (void)didReceiveControllerRequest:(NSNotification *)n;
@end

@implementation SampleProvider

#pragma mark - Overrides
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (id)init {
    self = [super init];
    if( self ) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveControllerRequest:) name:cControllerRequestNotification object:nil];
    }
    return self;
}

#pragma mark - Public API

+ (void)requestControllerForClassName:(NSString *)className completion:(RequestCallback)callback{

    NSDictionary *requestInfo = @{ cRequestClassNameKey : className, cRequestCallbackKey : [callback copy] };
    [[NSNotificationCenter defaultCenter] postNotificationName:cControllerRequestNotification object:requestInfo];
}

#pragma mark - Private API

- (UIViewController *)controllerForClassName:(NSString *)className {
    UIViewController *result = nil;
    Class controllerClass = NSClassFromString(className);
    if( (nil != controllerClass) && ([controllerClass isSubclassOfClass:[UIViewController class]]) ) {
        result = [[controllerClass alloc] init];
    }
    return result;
}

- (void)didReceiveControllerRequest:(NSNotification *)n {
    NSDictionary *requestInfo = [n object];
    NSString *className = requestInfo[cRequestClassNameKey];    
    RequestCallback callback = requestInfo[cRequestCallbackKey];

    UIViewController *result = [self controllerForClassName:className];

    if( nil != callback ) {
        callback(result);
    }
}

@end

/**
* SampleViewController Class
*/

@interface SampleViewController : UIViewController
@end
@implementation SampleViewController

#pragma mark - Overrides
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    NSString *className = @"ClassName";

    [SampleProvider requestControllerForClassName:className completion:^(UIViewController *result) {        
        if( nil != result ) {
            // Result is valid pointer, not a zombie.
            [self.navigationController pushViewController:result animated:YES];
            // Result is released, not nil.
        } else {
            NSLog(@"Unable to load controller with class name: %@", className);
        }
    }];
}
@end

为什么即使在显示 View 之后,我的 UINavigationController 也无法获得由 SampleProvider 的公共(public)类方法接收的回调 Controller 的所有权?

我看到以下行为:

  • 新的 Controller 类已正确分配并通过回调方法返回。进入回调后,结果参数指向有效内存。

  • 新 Controller 被推送到我的 UINavigationController 的导航堆栈中。

  • 调用新推送的 Controller 的“viewDidLoad”方法。

  • 检查 UINavigationController 的“viewControllers”属性时,数组中会引用新推送的 Controller 。

  • 新的推送 Controller 在 UINavigationController PushViewController:animated: 仍在执行时被释放。

  • 新 Controller 现在是僵尸。

感谢您的帮助。

最佳答案

我没有明确的答案,因为答案可能在您尚未发布的代码中 - 除了两个观察结果之外,您发布的代码看起来是有效的(这可能会引导您找到答案):

  • isKindOfClass 应该是 isSubclassOfClass 吗? -isKindOfClass:是一个 NSObject 上的实例方法,而不是类方法。

  • 在viewDidLoad期间同步调用pushViewController:似乎 危险的。 View 层次结构的状态很可能 那个时候还不稳定。这一插入应该是为了回应 我想还有其他一些离散事件。尝试插入(或 整个 requestControllerForClassName:) 异步通过 dispatch_async,作为测试,看看是否可以解决您的问题。

关于ios - 为什么 UINavigationController 无法使用 ARC 获取 block 回调参数的所有权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21316323/

相关文章:

ios - 旋转 avplayer 视频?

ios - tableView 不显示我的数据

iphone - 将所有日历日期插入到 uitableview 中。

objective-c - 使用自动引用计数 (ARC) 进行指针转换的问题

ios - objective c error "No visible @interface for ' NSString' declares the selector 'timeIntervalSinceDate:' "

ios - 从实时音频流创建 NSData

ios - 按日期分组 NSDictionary

ios - 在 CABasicAnimation 中控制速度

ios - Cocos2d Xcode - SLComposeViewController 黑屏

objective-c - ARC, block 中的ivars和通过捕获的 self 引用循环