收到低内存警告时,在进程中停止PerformRequestsWithTarget

时间:2013-03-06 13:29:28

标签: ios objective-c cocoa-touch

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];


}

1 个答案:

答案 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;
}