【问题标题】:Why is a UINavigationController failing to take ownership of a block callback parameter using ARC为什么 UINavigationController 无法使用 ARC 获得块回调参数的所有权
【发布时间】:2014-01-23 18:22:21
【问题描述】:

给出以下代码示例(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

为什么我的 UINavigationController 无法获得由 SampleProvider 的公共类方法接收的回调控制器的所有权,即使在显示视图之后也是如此?

我看到以下行为:

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

  • 新控制器被推送到我的 UINavigationController 的导航堆栈。

  • 调用新推送的控制器的“viewDidLoad”方法。

  • 检查 UINavigationController 的“viewControllers”属性时,新推送的控制器在数组中被引用。

  • 当 UINavigationController pushViewController:animated: 仍在执行时,新的推送控制器被释放。

  • 新控制器现在是僵尸。

感谢您的帮助。

【问题讨论】:

  • 实际推送可能计划在堆栈展开后在主运行循环上运行...所以您会在推送实际发生之前首先看到 -release...
  • 另一个选项——你的块是在不是主线程的线程上运行的吗?如果是,那么您可以将-pushViewController:animated:performBlockOnMainThread: 或类似的东西包围
  • 该块似乎正在同步执行,但是,我尝试使用主队列将 -pushViewController:animated: 与 GCD 包装在一起。这会在推送发生之前产生崩溃。这让我感到困惑,因为我希望 GCD 块增加对象的保留计数,从而防止其释放。
  • 我根据下面 Sean Cier 的回答更新了示例代码以纠正不相关的缺陷。
  • 您可以使用 Instruments 查看应用程序中每个对象的每个保留/释放的调用堆栈。也许您可以查看僵尸的保留/释放跟踪。

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


【解决方案1】:

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

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

  • 似乎在 viewDidLoad 期间同步调用 pushViewController 危险的。视图层次结构的状态很可能 当时还不稳定。这种推动应该发生在响应 我想是其他一些离散事件。尝试推动(或 整个 requestControllerForClassName :) 异步通过 dispatch_async,作为测试,看看是否能解决你的问题。

【讨论】:

  • 感谢您的回复。在我的实现中,推送发生在根控制器的视图加载很久之后。但从好的方面来说,我没有想到在 viewDidLoad 中进行推送的缺点。另外,您对isSubclassOfClass 的看法是正确的。当我写样本时,那个从我身边溜走了。
猜你喜欢
  • 2018-05-28
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
  • 2020-07-29
相关资源
最近更新 更多