在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];
}
答案 0 :(得分:2)
您无法停止正在运行的方法,但可以从中返回。
e.g。
- (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;
}