#import "ViewController.h"
@implementation A
- (instancetype)init
{
self = [super init];
if (self)
{
self.databaseQueue = dispatch_queue_create("someQueue", DISPATCH_QUEUE_SERIAL);
}
return self;
}
- (void) privateLogMethod
{
NSLog(@"private log method called");
[NSThread sleepForTimeInterval:1];
}
- (void) performSomeAction
{
for (NSInteger i = 0; i < 10; i++) {
dispatch_async(_databaseQueue, ^{
NSLog(@"inside for loop");
[self privateLogMethod];
});
}
}
- (void) dealloc
{
NSLog(@"removing A from memory");
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.someClassA = [[A alloc] init];
[self.someClassA performSomeAction];
[self performSelector:@selector(removeA) withObject:nil afterDelay:5];
}
- (void) removeA
{
NSLog(@"call to remove A from memory");
self.someClassA = nil;
}
@end
#import <UIKit/UIKit.h>
@interface A: NSObject
- (void) performSomeAction;
@property (nonatomic, strong) dispatch_queue_t databaseQueue;
@end
@interface ViewController : UIViewController
@property (nonatomic, strong) A* someClassA;
@end
如果我们看到上面的代码,则会有一个保留周期,因为class A
持有databaseQueue
且databaseQueue
持有self
。因此,当ViewController
在5秒后询问dealloc A
时,class A
会在deallocating
之前完成循环。以下是输出。
2017-09-14 14:21:06.774517+0530 testdealloc[72021:1812626] inside for loop
2017-09-14 14:21:06.774768+0530 testdealloc[72021:1812626] private log method called
2017-09-14 14:21:07.775218+0530 testdealloc[72021:1812626] inside for loop
2017-09-14 14:21:07.775480+0530 testdealloc[72021:1812626] private log method called
2017-09-14 14:21:08.778805+0530 testdealloc[72021:1812626] inside for loop
2017-09-14 14:21:08.779251+0530 testdealloc[72021:1812626] private log method called
2017-09-14 14:21:09.784467+0530 testdealloc[72021:1812626] inside for loop
2017-09-14 14:21:09.785089+0530 testdealloc[72021:1812626] private log method called
2017-09-14 14:21:10.790469+0530 testdealloc[72021:1812626] inside for loop
2017-09-14 14:21:10.790929+0530 testdealloc[72021:1812626] private log method called
2017-09-14 14:21:11.775522+0530 testdealloc[72021:1812575] **call to remove A from memory**
2017-09-14 14:21:11.796196+0530 testdealloc[72021:1812626] inside for loop
2017-09-14 14:21:11.796659+0530 testdealloc[72021:1812626] private log method called
2017-09-14 14:21:12.802018+0530 testdealloc[72021:1812626] inside for loop
2017-09-14 14:21:12.802483+0530 testdealloc[72021:1812626] private log method called
2017-09-14 14:21:13.804953+0530 testdealloc[72021:1812626] inside for loop
2017-09-14 14:21:13.805432+0530 testdealloc[72021:1812626] private log method called
2017-09-14 14:21:14.806252+0530 testdealloc[72021:1812626] inside for loop
2017-09-14 14:21:14.806604+0530 testdealloc[72021:1812626] private log method called
2017-09-14 14:21:15.807852+0530 testdealloc[72021:1812626] inside for loop
2017-09-14 14:21:15.808306+0530 testdealloc[72021:1812626] private log method called
2017-09-14 14:21:16.809550+0530 testdealloc[72021:1812626] **removing A from memory**
我的问题是:我们在代码中有一个保留周期,但是,这不会导致内存泄漏。在这种情况下,是否可以保留代码[因为它确实不会导致内存泄漏]?或者我应该在块中使用__weak weakSelf = self
然后weakSelf
来确保根本没有保留周期?
答案 0 :(得分:2)
你问:
这可以吗?
很大程度上,是的。
更准确地说,这取决于您的需求/意图。如果你需要这个继续运行让你的应用程序正常运行(例如,你可能正在转换并保存一些图像),那么你肯定需要一个强大的参考,以便它完成)。
但如果您不需要继续执行,那么您将使用weakSelf
模式。例如,假设您在视图控制器中有一系列调度任务,这些任务仅用于以后触发UI更新。在这种情况下,如果视图控制器被解除,您可能不需要运行这些块,并且您当然不希望它挂起到视图控制器以查看已被解雇的视图。 (您甚至可能希望在取消视图控制器时取消这些已分派的项目,但这是另一个主题。)
底线,这取决于你的意图。
答案 1 :(得分:1)
我是第二个@Rob。但寻找任何避免保留周期的机会。这对于一个简单的项目是可以的,但是在处理复杂的项目时...它会节省大量的时间来尝试调试/解决你不确定根本原因的问题。
这是一篇很好的文章,解释了不同类型的保留周期并避免它们。
https://www.cocoawithlove.com/2009/07/rules-to-avoid-retain-cycles.html