0

I'm implementing an iOS app. and found that sometimes the screen became black little by little. for example, in a view controller, there are a collection view, a button, a page controller, and sometimes I found the collection view became black(or invisible), only black background is shown, after 1-2 seconds, the button is gone, then the whole screen is black. but if I put the app into background, then bring it back, everything is ok.

here is the code:


- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSTimeInterval timeLeft = [UIApplication sharedApplication].backgroundTimeRemaining - 1.0;
        if(timeLeft &lt 0)
        {
            completionHandler(UIBackgroundFetchResultNoData);
        }
        //create new thread to do something, and sleep at current thread
        ...create new threads
        [NSThread sleep:timeLeft];
        completionHandler(UIBackgroundFetchResultNewData);
    });
}

the issue could be reproduced if I repeat the following actions several times:

  1. Do some thing
  2. Put app to background(press home button)
  3. Bring app back to foreground
  4. repeat 1 - 3

we found following error in organizer for our app: : CoreAnimation: warning, deleted thread with uncommitted CATransaction; set CA_DEBUG_TRANSACTIONS=1 in environment to log backtraces.

after added , I got the following log:


Myapp[11496] : CoreAnimation: warning, deleted thread with uncommitted CATransaction; created by:
    0   QuartzCore                          0x31ca6a75  + 268
    1   QuartzCore                          0x31ca6929  + 224
    2   QuartzCore                          0x31cabddb  + 86
    3   QuartzCore                          0x31cab9f7  + 150
    4   UIKit                               0x3203f919  + 344
    5   UIKit                               0x320bb11b  + 138
    6   UIKit                               0x322b0ebf  + 218
    7   UIKit                               0x322b1169  + 104
    8   UIKit                               0x322b1735  + 36
    9   Myapp                               0x002e538d __61-[AppDelegate application:performFetchWithCompletionHandler:]_block_invoke + 632
    10  libdispatch.dylib                   0x3a487d53  + 10
    11  libdispatch.dylib                   0x3a48d689  + 228
    12  libdispatch.dylib                   0x3a48d8dd  + 56
    13  libsystem_pthread.dylib             0x3a5b8c17 _pthread_wqthread + 298
    14  libsystem_pthread.dylib             0x3a5b8adc start_wqthread + 8

my questions is: how could application:performFetchWithCompletionHandler cause animation issue?

to answer questions: 1. I'm sure that phone is not going to sleep 2. source code. sorry the project is too big

disorderdev
  • 1,458
  • 1
  • 14
  • 28
  • Have you tried logging the view's frame coordinates to see if they happen to just get pushed up or down little by little while you click on something? Does this happen when you do nothing or whenever you do an action like clicking something? Try to change you view controller's background into a different color and see if it still shows the black background. if it's showing the view controller's background color, you definitely did something wrong in your code that makes your view elements get pushed up or down the screen. – tyegah123 Aug 15 '14 at 03:46
  • 1
    Are you sure it simply isn't your device going to sleep? It happens in stages like that. It dims then goes black. – rmaddy Aug 15 '14 at 03:47
  • 1
    Can you show us your code? – Enrico Susatyo Aug 15 '14 at 05:39
  • It would help if you'd post at least application:performFetchWithCompletionHandler: part(with removed sensitive info of course). – Timur Kuchkarov Aug 18 '14 at 03:31

3 Answers3

3

Check that you are working with UI on main thread only, this should be the case.

Edit: - I've seen this one, although never used it(written by Peter Steinberger).

Also this answer can help avoiding several more problems.

Community
  • 1
  • 1
Timur Kuchkarov
  • 1,155
  • 7
  • 21
  • do you have any method to check whether there is any UI update is not run on main thread? as the project is pretty big, review code is too expensive. – disorderdev Aug 18 '14 at 13:56
  • @disorderdev, please see edit. Although I've never used it, it should be helpful. – Timur Kuchkarov Aug 19 '14 at 00:42
  • Also it's interesting why do you need to sleep on current thread for remaining time? You should call completionHandler as soon as possible. – Timur Kuchkarov Aug 19 '14 at 00:54
0

The thread created to perform your fetch invokes a call to update your ui, when it finishes performing whatever task it is doing, and its no longer needed it is deallocated. UIUpdates MUST be performed on the main thread or they might not be performed right away.

Have you tried dispatching to the main thread whatever ui update you are performing there?

Pochi
  • 13,391
  • 3
  • 64
  • 104
0

It's hard to know what you your completion handler is doing, but if it's updating UI try to dispatch them on the main thread

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSTimeInterval timeLeft = [UIApplication sharedApplication].backgroundTimeRemaining - 1.0;
        if(timeLeft < 0)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                completionHandler(UIBackgroundFetchResultNoData);
            });

        }
        //create new thread to do something, and sleep at current thread
        ...create new threads
        [NSThread sleep:timeLeft];
        dispatch_async(dispatch_get_main_queue(), ^{
            completionHandler(UIBackgroundFetchResultNewData);
        });

    });
}
chrs
  • 5,906
  • 10
  • 43
  • 74