Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Can';不能得到保留周期_Ios - Fatal编程技术网

Ios Can';不能得到保留周期

Ios Can';不能得到保留周期,ios,Ios,我在apple.com上找到了这段tableview lazyloading的代码,但是无法获得保留周期的点,创建解析器弱指针需要什么,请帮助 ParseOperation *parser = [[ParseOperation alloc] initWithData:self.appListData]; parser.errorHandler = ^(NSError *parseError) { dispatch_async(dispatch_get_main_queue(), ^{

我在apple.com上找到了这段tableview lazyloading的代码,但是无法获得保留周期的点,创建解析器弱指针需要什么,请帮助

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"

如果在完成块中引用解析器,该块将保留它。由于解析器反过来会保留完成块,因此会得到一个保留周期:

       parser
      +---------------------------+
      |                           |
      |                           |
      |                           |
 +----+   completion block        |<-------+
 |    |  +---------------------+  |        |
 |    |  |                     |  |        | holds onto
 |    |  |                     |  |        |
 |    |  |                     +-----------+
 +------>|                     |  |
      |  |                     |  |
      |  |                     |  |
      |  |                     |  |
      |  |                     |  |
      |  +---------------------+  |
      |                           |
      +---------------------------+
解析器
+---------------------------+
|                           |
|                           |
|                           |
+----+完井区块| | ||
|  |                     |  |
|  |                     |  |
|  |                     |  |
|  |                     |  |
|  +---------------------+  |
|                           |
+---------------------------+

当您在完成块中使用弱指针时,您打破了这个循环,因为完成块不再阻止解析器被解除分配。

如果您在完成块中引用解析器,则块将保留它。由于解析器反过来会保留完成块,因此会得到一个保留周期:

       parser
      +---------------------------+
      |                           |
      |                           |
      |                           |
 +----+   completion block        |<-------+
 |    |  +---------------------+  |        |
 |    |  |                     |  |        | holds onto
 |    |  |                     |  |        |
 |    |  |                     +-----------+
 +------>|                     |  |
      |  |                     |  |
      |  |                     |  |
      |  |                     |  |
      |  |                     |  |
      |  +---------------------+  |
      |                           |
      +---------------------------+
解析器
+---------------------------+
|                           |
|                           |
|                           |
+----+完井区块| | ||
|  |                     |  |
|  |                     |  |
|  |                     |  |
|  |                     |  |
|  +---------------------+  |
|                           |
+---------------------------+

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

谢谢您的回答,请您解释一下为什么块会保留解析器如果我在完成块中引用解析器,解析器对象是在完成块之外创建的,我是ios的初学者,请帮助。这很简单,块始终保留您从中引用的对象。否则,他们很容易在区块完成前被解除分配。阅读,这并不难,而且会让你的生活更轻松。谢谢你的回答,你能解释一下为什么块会保留解析器吗?如果我在完成块中引用解析器,解析器对象是在完成块之外创建的,我是ios的初学者,请帮我这很简单,块始终保留从中参照的对象。否则,他们很容易在区块完成前被解除分配。阅读这本书,它并不难,而且会让你的生活更轻松。