title: How use sqlite + fdbm library with threading on the iPhone
body: <p>Related to <a href="https://stackoverflow.com/questions/1082554/">this SO question</a>, I want to put the data loading in the background.</p>
  
  <p>However, I get 'library routine called out of sequence' errors.</p>
  
  <p>In <a href="https://stackoverflow.com/questions/745565/how-to-properly-call-sqlite-functions-from-background-thread-on-iphone">this SO thread</a> say that the way is using NSOperation, but looking on the samples on the web I not know how that could solve the issue.</p>
  
  <p>I share a single sqlite connection with the singleton pattern:</p>
  
  <pre><code>@interface Db : NSObject {
      NSString *path;
      FMDatabase* theDb;
      BOOL isOpen;
  }
  
  @property (retain, nonatomic) FMDatabase *theDb;
  @property (retain, nonatomic) NSString *path;
  @property (nonatomic) BOOL isOpen;
  --------
  static Db *currentDbSingleton = nil;
  #pragma mark Global access
  
  +(id)currentDb {
      @synchronized(self) {
          if (!currentDbSingleton) {
              NSString *reason = NSLocalizedString(@"The database is not set globally",
                                                   @"Error Db: database is not set");
              NSException *e = [NSException exceptionWithName:@"DBError"                        
                                                       reason:reason;
                                                     userInfo:nil];
              @throw e;
          }
      }
      return currentDbSingleton;  
  }
  </code></pre>
  
  <p>So is harder open twice the same db....</p>
  
  <p>Any ideas?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I confirmed the error is in calling sqlite. I use FDBM as thin wrapper for calling it.</p>
  
  <p>I'm running 2 threads: the main and a background task for loading of the data. I run it this way:</p>
  
  <pre><code>- (void) fillCache:(NSString *)theTable {
      [NSThread detachNewThreadSelector:@selector(fillCacheBackground:)
                               toTarget:self
                             withObject:theTable];
  }
  
  - (void)loadComplete {
      [self.table reloadData];
  }
  
  - (void) fillCacheBackground:(NSString *)theTable {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      Db *db= [Db currentDb];
      [db beginTransaction];
          ..... STUFF HERE
      [db commitTransaction];
      //Tell our callback what we've done
      [self performSelectorOnMainThread:@selector(loadComplete) 
                             withObject:nil 
                          waitUntilDone:YES];
      [pool drain];
  }
  </code></pre>
  
  <p>The code  for the db interface is at <a href="http://code.google.com/p/chibiorm/source/browse/#svn/trunk/src" rel="nofollow noreferrer">http://code.google.com/p/chibiorm/source/browse/#svn/trunk/src</a> — specifically the Db.h/m that are the only units that interface with fdbm/sqlite.</p>
  
  <p>The error happend when try to call sqlite functions from FDBM.</p>
  
  <p>For example happend here:</p>
  
  <pre><code>-(void) checkError {
      if ([self.theDb hadError]) { // &lt;====ERROR HERE
          NSLog(@"Err %d: %@", [self.theDb lastErrorCode], [self.theDb]);
      }
  }
  </code></pre>
  
  <p>This call the FDBM code:</p>
  
  <pre><code>- (BOOL) hadError {
      int lastErrCode = sqlite3_errcode(db);
      return (lastErrCode &gt; SQLITE_OK &amp;&amp; lastErrCode &lt; SQLITE_ROW);
  }
  </code></pre>