[[UIApplication sharedApplication] backgroundTimeRemaining] must be used on the main thread


dhoerl
 

I tried using this method, but in a dispatch block on a background thread. Xcode told me it was a no-no - I got a purple warning that this must be used on the main thread.

WOW - so how can I annotate my properties or methods so users get the same warning???


 



On Oct 19, 2018, at 1:57 PM, dhoerl via Groups.Io <dhoerl@...> wrote:

WOW - so how can I annotate my properties or methods so users get the same warning???

I don’t think you can. The developer docs for the Main Thread Checker imply that there’s a hardcoded set of functions/methods it checks:

At app launch, the Main Thread Checker dynamically replaces the implementations of methods that should only be called on the main thread with a version that prepends the check. Methods known to be safe for use on background threads are excluded from this check.

That approach doesn’t seem amenable to customization.

—Jens


Dave Fernandes
 

Why do you need to update UI while the app is in the background?

On Oct 19, 2018, at 4:57 PM, dhoerl via Groups.Io <dhoerl@...> wrote:

I tried using this method, but in a dispatch block on a background thread. Xcode told me it was a no-no - I got a purple warning that this must be used on the main thread.

WOW - so how can I annotate my properties or methods so users get the same warning???


 



On Oct 19, 2018, at 3:15 PM, Dave Fernandes <dave.fernandes@...> wrote:

Why do you need to update UI while the app is in the background?

Background THREAD, not backgrounded app.

—Jens


Dave Fernandes
 

But is this property providing any useful information if the app is not in the background?

On Oct 19, 2018, at 7:11 PM, Jens Alfke <jens@...> wrote:



On Oct 19, 2018, at 3:15 PM, Dave Fernandes <dave.fernandes@...> wrote:

Why do you need to update UI while the app is in the background?

Background THREAD, not backgrounded app.

—Jens


Quincey Morris
 

On Oct 19, 2018, at 17:32 , Dave Fernandes <dave.fernandes@...> wrote:

But is this property providing any useful information if the app is not in the background?

In a way it does, because it is documented to return a very large number when the app is not in the background. It’s the answer to the question, “How much time does this app have left before it’s killed?”.

I think it’s a mistake to think that the main-thread requirement means that an API is involved with updating UI. It may just mean that the API is not thread-safe, and the only way to ensure thread safety is to serialize it on the main thread.


Dave Fernandes
 

My question was more for the OP. Why is this needed? It sounded like he/she wants to use it to update UI; and I think there may be a design issue there.

On Oct 19, 2018, at 8:56 PM, Quincey Morris <quinceymorris@...> wrote:

On Oct 19, 2018, at 17:32 , Dave Fernandes <dave.fernandes@...> wrote:

But is this property providing any useful information if the app is not in the background?

In a way it does, because it is documented to return a very large number when the app is not in the background. It’s the answer to the question, “How much time does this app have left before it’s killed?”.

I think it’s a mistake to think that the main-thread requirement means that an API is involved with updating UI. It may just mean that the API is not thread-safe, and the only way to ensure thread safety is to serialize it on the main thread.



dhoerl
 

Work has been queued to an impact printer queue, printing has started, want to insure the job finishes and that status info on the job has been saved. In all examples I could find - in particular Apple's code in the doc on this - they put the work in a background queue and send the final "end" message from that queue. 

In my case, I have a flag to monitor, so my code is a loop with "sleep(1);" then a test of the flag, etc. If I have enough time at the end, I sleep on the background queue for another few seconds to allow job status to be saved. I didn't think sleeping on the main queue was a good idea :-)

In any case, I was asking the shared Application how much longer I had. Now, I just grab the total at the beginning, and as I sleep decrement it. Its good enough - I am being told I have 3 minutes and I actually only need tens of seconds, in my case.