2014-02-19 34 views
0

在我的应用我有很长appWillEnterForeground:appDidEnterBackground之前被称为:完成

- (void)appDidEnterBackground:(NSNotification*)notif方法,

它需要1-2秒来执行。这导致了以下问题:如果我关闭应用程序,然后再次打开速度非常快,比

- (void)appWillEnterForeground:(NSNotification*)notif

-appDidEnterBackground完成之前被调用,从而导致崩溃 - 数据并不一致,或者是这样的。我不想调查我的数据中究竟出了什么问题,我想阻止这种情况的发生 - 我想等到appDidEnterBackground完成。

我的代码:

- (void)appDidEnterBackground:(NSNotification*)notif 
{ 
    [self processAppDidEnterBackgroundRoutines]; 

    NSLog(@"%s - end", __FUNCTION__); 
} 

- (void)processAppDidEnterBackgroundRoutines 
{ 
    // 1-2 seconds prosessing 
} 

- (void)appWillEnterForeground:(NSNotification*)notif 
{ 
    NSLog(@"%s - begin", __FUNCTION__); 
} 

席力图召

[self performSelectorOnMainThread:@selector(processAppDidEnterBackgroundRoutines) withObject:nil waitUntilDone:YES];

,但它并不能帮助出于某种原因 - appWillEnterForeground:processAppDidEnterBackgroundRoutines完成之前仍然被调用。

有没有人有别人的想法如何同步此调用?

回答

2

它会为你工作,把两个串行队列?

- (void)appDidEnterBackground:(NSNotification*)notif 
{ 
    dispatch_sync(serialQueue, ^() 
    { 
     [self processAppDidEnterBackgroundRoutines]; 
    }); 
} 

- (void)processAppDidEnterBackgroundRoutines 
{ 
    // 1-2 seconds prosessing 
} 

- (void)appWillEnterForeground:(NSNotification*)notif 
{ 
    dispatch_sync(serialQueue, ^() 
    { 
     // do your stuff 
    }); 
} 
0

您的问题似乎是因为您在appDidEnterBackground:方法中使用了performSelectorOnMainThread:。这会导致该选择器稍后运行,因为您已经在主线程上运行了。只需停止做performSelectorOnMainThread:,因为它是不必要的,是什么导致你的问题。

+0

哦,没错,使用performSelectorOnMainThread:没有意义。但只需调用-processAppDidEnterBackgroundRoutines - 这是我的问题发生的原始情况。 performSelectorOnMainThread:只是一个尝试解决它。编辑我的问题。 – Anastasia

相关问题