#import "Page3VC.h"
#import "DataUpdateOp.h"
@interface Page3VC ()
@property (nonatomic, strong) DataUpdateOp *updateOp;
@property (nonatomic, assign) BOOL refreshing;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
- (IBAction)onClickBtn:(id)sender
{
if(self.refreshing == NO)
{
self.refreshing = YES;
if(self.updateOp == nil)
{
self.updateOp = [[DataUpdateOp alloc] init];
}
[self.updateOp startWithDelegate:self withSelector:@selector(onDataAvailable:)];
}
}
- (void)onDataAvailable:(id)sender
{
self.refreshing = NO;
self.updateOp = nil;
UIImage *image = (UIImage *)sender;
self.imageView.image = image;
}
@implementation DataUpdateOp
- (void)startWithDelegate:(id)delegate withSelector:(SEL)selector
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *imgURL = [[NSURL alloc] initWithString:@"http://macdown.uranusjr.com/static/images/logo-160.png"];
NSData *data = [[NSData alloc] initWithContentsOfURL:imgURL];
UIImage *image = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
if(delegate && [delegate respondsToSelector:selector])
{
[delegate performSelector:selector withObject:image];
}
});
});
}
@end
[self.updateOp startWithDelegate:self withSelector:@selector(onDataAvailable:)]; 这里把self传入 且VC对updateOp有引用 这里会产生循环引用
===
更新
- (IBAction)onClickBtn:(id)sender
{
if(self.refreshing == NO)
{
self.refreshing = YES;
DataUpdateOp *updateOp = [[DataUpdateOp alloc] init];
[updateOp startWithDelegate:self withSelector:@selector(onDataAvailable:)];
}
}
- (void)onDataAvailable:(id)sender
{
self.refreshing = NO;
UIImage *image = (UIImage *)sender;
self.imageView.image = image;
}
采用局部变量 updateOp 会持有VC 但是VC不会持有updateOp 解决了循环引用问题 但是 updateOp 无法被其他地方使用
一种case 网络请求在排队中 APP在使用中切换页面 VC理想情况下应该立即回收 但是VC被网络操作持有 所以不会立即被回收 虽然没有循环引用 但是会造成内存使用峰值
解决方法
委托对操作是强引用
操作对委托是弱引用
操作引入cancel方法
改进如下
- (IBAction)onClickCloseVC:(id)sender
{
[self dismissViewControllerAnimated:NO completion:nil];
}
- (IBAction)onClickBtn:(id)sender
{
if(self.refreshing == NO)
{
self.refreshing = YES;
if(self.updateOp == nil)
{
self.updateOp = [[DataUpdateOp alloc] init];
}
self.updateOp.delegate = self;
[self.updateOp start];
}
}
- (void)onDataAvailable:(id)sender
{
self.refreshing = NO;
self.updateOp = nil;
UIImage *image = (UIImage *)sender;
self.imageView.image = image;
}
- (void)dealloc
{
NSLog(@"VC dealloc");
if(self.updateOp != nil)
{
[self.updateOp cancel];
}
}
#import "DataUpdateOp.h"
@interface DataUpdateOp ()
@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
@end
@implementation DataUpdateOp
- (void)start
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *imgURL = [[NSURL alloc] initWithString:@"http://macdown.uranusjr.com/static/images/logo-160.png"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:imgURL];
NSURLSession *session = [NSURLSession sharedSession];
self.downloadTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
UIImage *image = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
id<DataUpdateOpDelegate> delegate = self.delegate;
if(!delegate)
{
}
else
{
[delegate onDataAvailable:image];
}
});
}];
[self.downloadTask resume];
});
}
- (void)cancel
{
NSLog(@"request cancel");
[self.downloadTask cancel];
self.delegate = nil;
}
@end
补充(http://www.cocoachina.com/bbs/read.php?tid=1726514#)
id<DataUpdateOpDelegate> delegate = self.delegate;
self.delegate 本身是 weak
处理过程中需要保证一直有效
=右边赋值 强持有一下
保证在 block 执行期间 delegate 一直有效