title: Retrieving data from web service and populating SQLite3 database efficiently
body: <p>I am currently developing an app that that will be used by association members that are going to a large annual conference.  </p>
  
  <p>The app will pull data from a database that is created by the app and populate it via a web service.  The web service is split into 8 pages (this will likely go up).  Each page represents a table in the database.  The app will have several table views that will be populated by data in one or more of the tables in the database.</p>
  
  <p>What I need is a the best method for going through the list of tables, connecting to their respective web service pages and then populating the respective database tables.  This updating needs to take place in the background so the UI doesn't become unresponsive and/or show a downloading/updating/waiting kind of status.</p>
  
  <p>So far I have a static array of the table names and have a loop that goes through the array and appends a URL string with the names, for example:</p>
  
  <pre><code>-(void)startUpdate
  {
  NSArray* tableNames =  @[@"speaker", @"exhibitor", @"workshop", @"workshopspeakers", @"schedule", @"location", @"feedback", @"note", @"usage", @"user"];
  
  NSUInteger loopCount = tableNames.count;
  
  for (int i = 0; i &lt; loopCount; ++i){
      NSString *tableName = [tableNames objectAtIndex:i];   
      [self fetchObjectsWithTableName:[tableName mutableCopy] completion:^(NSArray* objects, NSError*error){
          if (error) {
          } else {
          }
      }];
  }
  }
  </code></pre>
  
  <p>fetchObjectsWithTableName method then has the connections and retrieves the data:</p>
  
  <pre><code>-(void)fetchData:(NSString *)tableName
                      withCompletion:(completion_t)completionHandler
  {
  NSString *currentURL = [NSString stringWithFormat:@"https://testapi.someURL.com/api/congress/%@", tableName];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:currentURL]];
  [request addValue:@"application/json" forHTTPHeaderField:(@"Accept")];
  [NSURLConnection sendAsynchronousRequest:request
                                     queue:[[NSOperationQueue alloc] init]
                         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
   {
       NSError* err = error;
       NSArray* objects; // final result array as a representation of JSON Array
       if (response) {
           NSHTTPURLResponse *newResp = (NSHTTPURLResponse*)response;
           if (newResp.statusCode == 200) {
               NSLog(@"FetchData - Status code = %li", (long)newResp.statusCode);
               if ([data length] &gt;0 &amp;&amp; error == nil)
               {
                   NSError* localError;
                   objects = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&amp;error];
                   if (objects) {
                       if (completionHandler) {
                           completionHandler(objects, nil);
                       }
                       //NSLog(@"Objects in current table - %@ = %@", tableName, objects);
                       [self.tables addObject:objects];
                     //  NSLog(@"Tables now = %@", self.tables);
                       NSLog(@"FetchData - Objects in current table - %@ = %lu", tableName, (unsigned long)objects.count);
                       return;
                   } else {
                       err = localError;
                   }
               } else {
                   NSLog(@"FetchData - objects is empty");
                   return;
                  // err = ...
               }
           }
           NSLog(@"FetchData - Response code not 200@");
       }
       if (objects == nil) {
           NSLog(@"FetchData - Nothing found in table: %@", tableName);
           //assert(err);
           if (completionHandler) {
               completionHandler(nil, err);
           }
       }
  }];
  
  }
  </code></pre>
  
  <p>This currently goes through the array of table names, makes a connection based on each one and pulls back JSON data and stores it in a temporary array 'objects'.  I think what I need now is that in each iteration of this 'objects' array is copied to the relevant table in the database, i.e. 'speaker' table name makes a connection: <a href="https://testapi.someURL.com/api/congress/speaker" rel="nofollow">https://testapi.someURL.com/api/congress/speaker</a> and the JSON is entered into the database under the table 'speaker'.  How and where do I do that?  Will I need to add a completion handler to startUpdate?  If so, how?  I don't understand completion handlers despite looking at several examples.  Thanks.</p>