Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're right — you should call the completion handler only when your fetch is actually complete. Otherwise iOS will probably put your application back to sleep before the connection completes, and apps shouldn't actually be able to determine <code>UIBackgroundFetchResultNewData</code> versus <code>UIBackgroundFetchResultNoData</code> or <code>UIBackgroundFetchResultFailed</code> until then anyway. How do you know your connection will succeed?</p> <p>You need to keep hold of the completionHandler and call it only once the connection has finished. Most likely you'll want to add an instance variable to your delegate, copy the completion handler into there, and call it when you're done.</p> <p>Aside: <code>connection:didReceiveData:</code> doesn't signal the end of a request. Per the documentation:</p> <blockquote> <p>Sent as a connection loads data incrementally.</p> <p>[...]</p> <p>The delegate should concatenate the contents of each data object delivered to build up the complete data for a URL load.</p> </blockquote> <p>You may receive any number of calls and the net result of the URL connection is the accumulation of all of them.</p> <p>EDIT: you store a block by creating an instance variable of the correct type and <em>copying</em> the block to it. Blocks have unusual semantics because, unlike every other kind of Objective-C object, they're initially created on the stack. The net effect is just that you always <code>copy</code> them. If they're on the stack when you copy then they end up on the heap. If they're already on the heap then the copy just acts as a retain, since blocks are always immutable anyway.</p> <p>So:</p> <pre><code>@implementation XXMDYourClass { // syntax follow the C rule; read from the centre outwards void (^_completionHandler)(UIBackgroundFetchResult); } - (id)initWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { self = [super init]; if(self) { // keep the block by copying it; release later if // you're not using ARC _completionHandler = [completionHandler copy]; } return self; } - (void)somethingThatHappensMuchLater { _completionHandler(UIBackgroundFetchResultWhatever); } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload