gpt4 book ai didi

ios - 无法获取保留周期

转载 作者:行者123 更新时间:2023-11-29 13:00:03 25 4
gpt4 key购买 nike

我从apple.com上找到了这段uitableview延迟加载的代码,但是没有得到retain cycle的要点,创建解析器的弱指针需要什么,请帮助。

ParseOperation *parser = [[ParseOperation alloc] initWithData:self.appListData];

parser.errorHandler = ^(NSError *parseError) {
dispatch_async(dispatch_get_main_queue(), ^{
[self handleError:parseError];
});
};
// Referencing parser from within its completionBlock would create a retain
// cycle.
__weak ParseOperation *weakParser = parser;

parser.completionBlock = ^(void) {
if (weakParser.appRecordList) {
// The completion block may execute on any thread. Because operations
// involving the UI are about to be performed, make sure they execute
// on the main thread.
dispatch_async(dispatch_get_main_queue(), ^{
// The root rootViewController is the only child of the navigation
// controller, which is the window's rootViewController.
RootViewController *rootViewController = (RootViewController*) [(UINavigationController*)self.window.rootViewController topViewController];

rootViewController.entries = weakParser.appRecordList;

// tell our table view to reload its data, now that parsing has completed
[rootViewController.tableView reloadData];
});
}

// we are finished with the queue and our ParseOperation
self.queue = nil;
};

[self.queue addOperation:parser]; // this will start the "ParseOperation"

最佳答案

如果您在完成 block 中引用解析器,该 block 将保留它。由于解析器依次保留完成 block ,因此您会得到一个保留周期:

       parser
+---------------------------+
| |
| |
| |
+----+ completion block |<-------+
| | +---------------------+ | |
| | | | | | holds onto
| | | | | |
| | | +-----------+
+------>| | |
| | | |
| | | |
| | | |
| | | |
| +---------------------+ |
| |
+---------------------------+

当你在完成 block 中使用弱指针时,你打破了这个循环,因为完成 block 不再阻止解析器被释放。

关于ios - 无法获取保留周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20001674/

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