上篇iOS - 循环引用markdown
咱们知道,循环引用即: 当两个对象A和B, 分别强引用对方,那么就会产生循环引用。即A释放的时候必须先释放B,而B释放的时候必须释放A。致使谁也不能释放 而打破循环引用的方法就是其中一方弱引用另外一方async
#1. Block是否必定会形成循环引用 根据上篇文咱们知道,循环引用的关键是:相互强引用,若是没有达到这一必要条件,则block不会形成循环引用post
两个简单的例子:动画
@interface BlockViewController ()
@property (nonatomic, copy) NSString *nickname;
@property (nonatomic, copy) void(^ testBlock1)(void);
@property (nonatomic, copy) void(^ testBlock2)(void);
@end
@implementation BlockViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
// 会形成循环引用,由于self->Block, block -> self
self.testBlock1 = ^{
NSLog(@"== self = %p",self);
};
//不会形成循环引用,由于只有self->block
self.testBlock2 = ^{
};
}
- (void)dealloc {
NSLog(@"dealloc = %@", self);
}
@end
复制代码
编译成cpp源码:atom
testBlock0spa
struct __BlockViewController__viewDidLoad_block_impl_0 {
struct __block_impl impl;
struct __BlockViewController__viewDidLoad_block_desc_0* Desc;
BlockViewController *self;
__BlockViewController__viewDidLoad_block_impl_0(void *fp, struct __BlockViewController__viewDidLoad_block_desc_0 *desc, BlockViewController *_self, int flags=0) : self(_self) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __BlockViewController__viewDidLoad_block_func_0(struct __BlockViewController__viewDidLoad_block_impl_0 *__cself) {
BlockViewController *self = __cself->self; // bound by copy
NSLog((NSString *)&__NSConstantStringImpl__var_folders_44_1ht3l6g55dv59_5s62wsv_bm0000gn_T_BlockViewController_439f5f_mi_0,self);
}
((void (*)(id, SEL, void (*)()))(void *)objc_msgSend)((id)self, sel_registerName("setTestBlock1:"), ((void (*)())&__BlockViewController__viewDidLoad_block_impl_0((void *)__BlockViewController__viewDidLoad_block_func_0, &__BlockViewController__viewDidLoad_block_desc_0_DATA, self, 570425344)));
复制代码
testBlock1:code
struct __BlockViewController__viewDidLoad_block_impl_1 {
struct __block_impl impl;
struct __BlockViewController__viewDidLoad_block_desc_1* Desc;
__BlockViewController__viewDidLoad_block_impl_1(void *fp, struct __BlockViewController__viewDidLoad_block_desc_1 *desc, int flags=0) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __BlockViewController__viewDidLoad_block_func_1(struct __BlockViewController__viewDidLoad_block_impl_1 *__cself) {
}
((void (*)(id, SEL, void (*)()))(void *)objc_msgSend)((id)self, sel_registerName("setTestBlock2:"), ((void (*)())&__BlockViewController__viewDidLoad_block_impl_1((void *)__BlockViewController__viewDidLoad_block_func_1, &__BlockViewController__viewDidLoad_block_desc_1_DATA)));
复制代码
或者以下:也不会形成循环引用,虽然block持有self,但self没有持有blockorm
BlockViewController2 *vc = [[BlockViewController2 alloc] init];
[self.navigationController pushViewController:vc animated:true];
vc.testBlock10 = ^{
NSLog(@"-----vc.testBlock10 = %p", self);
};
复制代码
不会,+ (void)animateWithDuration:(NSTimeInterval)duration animations:
是类方法,当前控制器没法强引用一个类,因此循环引用没法构成对象
不会 , self持有了Masonry,可是Masonry源码中并无持有View,,若是将源码的block(constrainMaker)
改为self.block = block(constrainMaker)
,那么此时view才会持有block,才会形成循环引用get
self.testButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:self.testButton];
[self.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.centerY.equalTo(self.view);
make.size.mas_equalTo(CGSizeMake(100, 50));
}];
Masonry源码:
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
self.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
block(constraintMaker);
return [constraintMaker install];
}
复制代码
项目中用到AFN的代码
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
[manager POST:completeURL parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
复制代码
AFN源码
- (NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(id)parameters
progress:(void (^)(NSProgress * _Nonnull))uploadProgress
success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure
{
NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"POST" URLString:URLString parameters:parameters uploadProgress:uploadProgress downloadProgress:nil success:success failure:failure];
[dataTask resume];
return dataTask;
}
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
{
NSError *serializationError = nil;
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
if (serializationError) {
if (failure) {
dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
failure(nil, serializationError);
});
}
return nil;
}
__block NSURLSessionDataTask *dataTask = nil;
dataTask = [self dataTaskWithRequest:request
uploadProgress:uploadProgress
downloadProgress:downloadProgress
completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
if (error) {
if (failure) {
failure(dataTask, error);
}
} else {
if (success) {
success(dataTask, responseObject);
}
}
}];
return dataTask;
}
复制代码
可见: self并无持有manager, dataTask也没有持有success和failure这两个block,即便block中持有了self。也不会构成循环引用