0

ViewDidAppear

[self performSelectorInBackground:@selector(generatePdf) withObject:nil];

叫做。这会在加载视图后立即生成一个大 PDF。为了考虑内存不足的问题,我可以在启动后停止该进程吗?使用

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(generatePdf) object:nil];

不起作用,大概是因为视图一出现就已经调用了它。如果应用程序收到,我想停止这个过程,didReceiveMemoryWarning所以我正在寻找一种解决方案,可以在我的- (void)didReceiveMemoryWarning方法中完全停止它

- (void)viewDidAppear:(BOOL)animated
{


 LogCmd();
[super viewDidAppear:animated];
if (self.pdfData == nil) {



    // Generate PDF

    [self performSelectorInBackground:@selector(generatePdf) withObject:nil];
   }
}

 ....some code


- (void)didReceiveMemoryWarning {



[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(generatePdf) object:nil];


}
4

1 回答 1

2

您不能停止正在运行的方法,但可以从中返回。

例如

- (BOOL)generatePdf{
    // some task

    if (memoryWarning){
        return NO;
    }

    // some Task

    if (memoryWarning){
        return NO;
    }

    //some Task

    if (memoryWarning){
        return NO;
    }

    //some Task

    //PDF has been successfully generated
    return YES;
}
于 2013-03-06T14:09:51.870 回答