title: sqlite3_prepare_v2 crash - Already running previous query
body: <p>I have written a query that fires on <code>viewWillAppear</code>. On coming back to that screen with a quick navigation is resulting in crash at <code>sqlite3_prepare_v2</code>.<br>
  What I am assuming is that the crash is due to the previous query not finalized.<br>
  I am calling the database operation using <code>dispatch_async</code> as I am first updating remote data and then saving it in database.<br>
  The code looks like this :  </p>
  
  <pre><code>dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
  
                   dispatch_async(myQueue, ^{
                      [self parseActivityResponse:data];  // Sqlite operation here
                       dispatch_async(dispatch_get_main_queue(), ^{
                           // Update the UI
                           if(loaderImageview)
                              [loaderImageview removeFromSuperview];
                           [tableActivities reloadData];
  
                       });
                   }); 
  </code></pre>
  
  <p>The query is as follows :  </p>
  
  <pre><code>- (NSMutableArray*)GetAllInvitations:(NSString*)ActivityId
  {
      NSMutableArray *arrInvitations = [[NSMutableArray alloc]init];
  
      sqlite3_stmt *statement = nil;
  
      @try {
          const char *sql = "SELECT * FROM Invitation where ActivityId = ?";
  
          if (sqlite3_prepare_v2(database, sql, -1, &amp;statement, NULL) == SQLITE_OK) {
  
              sqlite3_bind_text(statement, 1, [ActivityId UTF8String], -1, SQLITE_TRANSIENT);
              while (sqlite3_step(statement) == SQLITE_ROW) {
  
                  char *dbString;
  
                  Invitation *invitation = [[Invitation alloc]init];
  
                  dbString = (char *)sqlite3_column_text(statement, 0);
                  NSString *Id = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                  [invitation setId:Id];
  
                  dbString = (char *)sqlite3_column_text(statement, 1);
                  NSString *childName = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                  [invitation setChildName:childName];
  
                  dbString = (char *)sqlite3_column_text(statement, 2);
                  NSString *response = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                  [invitation setResponse:response];
  
                  dbString = (char *)sqlite3_column_text(statement, 3);
                  NSString *invitationId = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                  [invitation setInvitationId:invitationId];
  
                  dbString = (char *)sqlite3_column_text(statement, 4);
                  NSString *isCoGaurdian = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                  [invitation setIsCoGaurdian:isCoGaurdian];
  
                  dbString = (char *)sqlite3_column_text(statement, 5);
                  NSString *activityId = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                  [invitation setActivityId:activityId];
  
                  dbString = (char *)sqlite3_column_text(statement, 6);
                  NSString *picture = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                  [invitation setPicture:picture];
  
                  dbString = (char *)sqlite3_column_text(statement, 7);
                  NSString *invitationUserId = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                  [invitation setInvitedUserId:invitationUserId];
  
                  dbString = (char *)sqlite3_column_text(statement, 8);
                  NSString *roleId = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                  [invitation setRoleId:roleId];
  
                  dbString = (char *)sqlite3_column_text(statement, 9);
                  NSString *status = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                  [invitation setStatus:status];
  
                  [arrInvitations addObject:invitation];
              }
          }
          else
          {
              NSLog(@"Failed to create table: %s", sqlite3_errmsg(database));
          }
  
  
      }
      @catch (NSException *exception) {
  
          NSLog(@"Error in GetAllInvitations : %@", exception.description);
      }
      @finally {
  
          sqlite3_finalize(statement);
      }
  
      return arrInvitations;
  }  
  
  
  - (NSMutableArray*)GetAllActivities{
  
      NSMutableArray *arrProjects = [[NSMutableArray alloc]init];
  
      sqlite3_stmt *statement = nil;
  
      @try {
          const char *sql = "SELECT * FROM Activity";
  
          if (sqlite3_prepare_v2(database, sql, -1, &amp;statement, NULL) == SQLITE_OK) {
              NSLog(@"success");
              while (sqlite3_step(statement) == SQLITE_ROW) {
  
                  .........
  
                  activity.arrInvitations = [self GetAllInvitations:activityId];
  
                  [arrProjects addObject:activity];
              }
          }
          else
          {
              NSLog(@"Prepare-error #%i: %s", sqlite3_prepare_v2(database, sql, -1, &amp;statement, NULL), sqlite3_errmsg(database));
          }
      }
      @catch (NSException *exception) {
  
          NSLog(@"Error in GetAllActivities : %@", exception.description);
      }
      @finally {
  
          sqlite3_finalize(statement);
      }
  
      return arrProjects;
  }
  </code></pre>
  
  <p>I am calling <strong>GetAllActivities</strong> from <code>viewWillAppear</code> :  </p>
  
  <pre><code>-(void) viewWillAppear:(BOOL)animated
  {
      [super viewWillAppear:animated];
      [self reloadData];
  }
  
  -(void)reloadData
  {
      [self getRemoteActivities:^(BOOL finished) {
          if(finished){
              if([TGProjectHandler hasExistingSession])
              {
                  [self getActivities];
  
              }
              else
                  [self login];   
          }
      }];
  }
  </code></pre>
  
  <p>Your thoughts and solution ? </p>