Search code examples
iosobjective-cmemorydestroy

Pop a viewController which has asnyc task, will it destory before the task finish in iOS?


When I pop my viewcontroller, in it's viewWillDisappear method,I localize the viewcontroller's data to sandbox,and will the localization finish then system destroy viewcontroller?

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    // localization method
    [self localDataToSandbox];
}

- (void)localDataToSandbox {

    if (self.buyDataSource.count > 0) {

        NSMutableArray *arr_tmp;

        if (self.buyDataSource.count > 20) {
            arr_tmp = [[self.buyDataSource subarrayWithRange:NSMakeRange(0, 20)] mutableCopy];
        }else {
            arr_tmp = self.buyDataSource;
        }

        CacheManager *manager = [CacheManager sharedManager];
        [manager cacheModelArray:arr_tmp toPath:[Util getTmpDirectory] withName:buy_cache];
    }
}

UPDATE

As we know, if vc1 push to vc2, and vc2 pop to vc1, the vc2 will be recycled by system.

my question is if when vc2 pop to vc1, will the method in viewWillDisappear interrupt?


Solution

  • If localDataToSandbox would have a closure/block that captures self, it would be invoked back to that closure while in the other case (your case - when you wouldn't have that closure to capture self), it would be removed from the stack nav hierarchy and there is no guaranty that localDataToSandbox will finish its actual runtime.

    Meaning, using a closure would solve your issue and self would be captured.