0

如果 UIViewController 运行如下所示的后台线程操作并在完成之前被释放(假设它是从 UINavigationController 弹出的),后台线程是否停止?确保后台线程完成的好方法是什么?我担心的是,如果用户在字段中输入一个值,并且在解除分配视图控制器之前数据库没有更新,那么数据库将不会更新。

    // Doing something on the main thread

dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
dispatch_async(myQueue, ^{
    // Perform long running process

    dispatch_async(dispatch_get_main_queue(), ^{
        // Update the UI

    });
}); 

// Continue doing other stuff on the 
// main thread while process is running.
4

1 回答 1

1

不,后台线程不会停止。

这是因为 GCD 将保留该块,直到该块完成。一旦你调度你的块,你的视图控制器就变得无关紧要了。

此外,如果您引用了任何变量,无论是视图控制器本身还是定义块的方法范围的本地变量,这些变量也将被保留。

因此,即使您的视图控制器不再位于导航堆栈中,它也有可能不会被释放。

于 2013-04-11T07:05:13.263 回答