gpt4 book ai didi

ios - self.completionBlock = ^{} 和 (void)(^completionBlock)(void) = ^{} 之间的区别

转载 作者:可可西里 更新时间:2023-11-01 05:52:29 26 4
gpt4 key购买 nike

最近在关注 Apple 文档之后

Screenshot from an Apple Developer Video我使用以下约定来避免保留周期问题。

__weak __typeof(self) weak_self = self;
void(^completionBlock)(void) = ^(){

__typeof(self) strong_self = weak_self;
if (strong_self) {

if (strong_self->_completion != NULL) {
strong_self->_completion();
}
}
};

但是发现这段代码崩溃了,因为 self 在调用 block 之前被释放了。

当我使用以下内容时发现它正在工作。

__block __typeof(self) block_self = self;
void(^completionBlock)(void) = ^(){

if (block_self->_completion != NULL) {
block_self->_completion();
}
};

现在我很困惑什么时候应该使用 __weak 引用。仅在下面的“self.completionBlock

情况下
__weak __typeof(self) weak_self = self;
self.completionBlock = ^(){

if (weak_self->_completion != NULL) {
weak_self->_completion();
}
};

有关此条件的任何说明对我都非常有用。

我的实现代码如下。

============================================= ==

文件 MyViewController

@implementation MyViewController

//starting point
- (void)myPushMethod {
__weak __typeof(self) weak_self = self;
MyViewControllerTransitioning *delegateObj = [[MyViewControllerTransitioning alloc] initWithCompletion:^{

//resetting the delegate
__typeof(self) strong_self = weak_self;
if (strong_self) {
strong_self.navigationController.delegate = nil;
}
}];
self.navigationController.delegate = delegateObj;
[self.navigationController pushViewController:someViewController animated:_animated];
//it is found that delegateObj is getting deallocated while reaches this point
}

@end

============================================= ==

文件 MyViewControllerTransitioning

@implementation MyViewControllerTransitioning

- (instancetype)initWithCompletion:(completionHandler)completionHandler
{
if(self = [super init])
{
if (completionHandler != NULL) {
_completion = completionHandler;
}
}
return self;
}

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
{
id<UIViewControllerAnimatedTransitioning> animationTransitioning = nil;

//crashes here
__block __typeof(self) block_self = self;
void(^completionBlock)(void) = ^(){

if (block_self->_completion != NULL) {
block_self->_completion();
}
};

//showing presentation-up animation if Push
if (operation == UINavigationControllerOperationPush) {
animationTransitioning = [[MyAnimator alloc] initWithAnimationCompletion:completionBlock];
}
return animationTransitioning;
}

- (void)dealloc{
//dealloc is called before invoking completionBlock
}

@end

============================================= ==

文件 MyAnimator

@implementation MyAnimator

- (instancetype)initWithAnimationCompletion:(presentationUpCompletion)completionHandler
{
if(self = [super init])
{
if (completionHandler != NULL) {
_completion = completionHandler;
}
}
return self;
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return my_duration;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {

//animation logic

[UIView animateWithDuration:duration animations: ^{
//animation logic
} completion: ^(BOOL finished) {
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}

- (void)animationEnded:(BOOL) transitionCompleted {
if (transitionCompleted && _completion != NULL) {
_completion();
}
}

@end

最佳答案

在下面的原始回答中,我介绍了标准的 weakSelf 模式(以及 weakSelf-strongSelf“舞蹈”)。我的结论是,您引用的演示幻灯片关于 weakSelf 模式是绝对正确的(尽管该演示文稿在风格上已经过时)。

您随后提供了一个更完整的代码示例,结果发现它遇到了一个不同的、不相关的问题。 (更糟糕的是,这个问题只有在解决了强引用循环时才会出现。大声笑。)最重要的是,代码将导航 Controller 的委托(delegate)设置为超出范围的本地对象。因为导航 Controller 不保留它的委托(delegate),所以您最终会得到一个指向这个已释放对象的悬空指针。

如果您保留自己对此委托(delegate)对象的 strong 引用(防止它被释放),问题就会消失。


我的原始答案如下。


你说:

used the following conventions to avoid retain cycle issues.

__weak __typeof(self) weak_self = self;

void(^completionBlock)(void) = ^(){
__typeof(self) strong_self = weak_self;
if (strong_self) {
if (strong_self->_completion != NULL) {
strong_self->_completion();
}
}
};

But this code is found to be crashing because self is getting deallocated before invoking the block.

不,这是一种很常见的模式(常被戏称为“弱我强我舞”)。这没有错。如果它崩溃了,那是因为其他原因。

当然,我会使用现代命名约定(weakSelfstrongSelf 而不是 weak_selfstrong_self) .我会删除 __typeof 开头的 __。而且我不会倾向于取消引用和 ivar,而是使用属性。所以,我可能会得到类似这样的结果:

__weak typeof(self) weakSelf = self;

void(^completionBlock)(void) = ^(){
typeof(self) strongSelf = weakSelf;
if (strongSelf) {
if (strongSelf.completion != NULL) {
strongSelf.completion();
}
}
};

但是,如果它崩溃了,那么您还有其他问题。坦率地说,您有一个调用 block 的 block ,因此很难猜测您的问题出在哪里,但问题不在于“弱 self ”模式。

稍后您继续建议使用:

__block __typeof(self) block_self = self;

这并不像您认为的那样。 “weak self”模式的目标是打破强引用循环(以前称为 retain cycle),但是在 ARC 中,这个 __block 引用没有解决那个强引用循环。请注意,在非 ARC 代码中,此 block_self 模式用于中断保留循环,但它不会在 ARC 中完成这项工作。

最后,你继续建议:

__weak __typeof(self) weak_self = self;
self.completionBlock = ^(){
if (weak_self->_completion != NULL) {
weak_self->_completion();
}
};

这有两个严重的问题。首先,您正在取消引用一个弱变量。如果 weak_selfnil,它就会崩溃。其次,即使您通过使用属性访问器方法而不是取消引用 ivars 来解决这个问题,您也会遇到另一个问题,即竞争条件。


最重要的是,该演示文稿中的“弱 self ”模式是正确的。同样,您第一个例子中的“弱 self , strong self 舞蹈”也是正确的。如果它为您崩溃,您必须向我们提供 MCVE这重现了问题。但在 ARC 代码中,这是一个完全有效的模式。

关于ios - self.completionBlock = ^{} 和 (void)(^completionBlock)(void) = ^{} 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29488965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com