What to do when users generate the same action several times, waiting for downloads?

I am developing an iPhone application. The user is looking for something. We take data from the network. Then we update the table.

Pseudo code will be

[DoThisAtbackground ^{
  LoadData ();
  [DoThisAtForeground ^{
    UpdateTableAndView();
  }];
}];

How about having a different user search before the first search.

What is the standard way to solve the problem?

  • Keep track of which thread is still running, and refresh the table when all threads are complete?
  • Refresh a view every time a thread ends?

How exactly do we do this?

+5
source share
3 answers

iOS. Apple , , , .

, :

  • : " , ." , , , , ".
  • : " , . , , ."

, , , , - . , , - , , , . , , , - ! , - Safari iOS: Google , , , .

, .

+2

REST , , , . , , , - . , ASIHTTPRequest

- (void)serverPerformDataRequestWithQuery:(NSString *)query andDelegate:(__weak id <ServerDelegate)delegate {
  [currentRequest setFailedBlock:nil];
  [currentRequest cancel];
  currentRequest = [[ASIHTTPRequest alloc] initWithURL:kHOST];
  [currentRequest startAsynchronous];
}

, SQLite, .

+2

NSOperationQueue , . -, ... .

GCD, , NSOperationQueue.

, , -.

, , , .

, I/O, , dispatch_io . Monk : " ".

:

- (void)userRequestedNewSearch:(SearchInfo*)searchInfo {
    // Assign this operation a new token, that uniquely identifies this operation.
    uint32_t token = [self nextOperationToken];

    // If your "loading" API has an external abort mechanism, you want to keep
    // track of the in-flight I/O so any existing I/O operations can be canceled
    // before dispatching new work.

    dispatch_async(myQueue, ^{
        // Try to load your data in small pieces, so you can exit as early as
        // possible.  If you have to do a monolithic load, that OK, but this
        // block will not exit until that stops.  
        while (! loadIsComplete) {
            if ([self currentToken] != token) return;
            // Load some data, set loadIsComplete when loading completes
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            // One last check before updating the UI...
            if ([self currentToken] != token) return;

            // Do your UI update operations
        });
    });

}

, . NSOperationQueue, cancelAllOperations, , .

0

All Articles