Search code examples
xcodemacossqlitefmdb

"Too many files open" issue used by FMDatabase/SQLite3


I am using the FMDatabase wrapper for SQLite3 in my OSX App. I do tons of inserts into the database:

FMResultSet *results;
results= [db executeQuery:@"select count(*) from `items` where key = ?",[keyPath lowercaseString],nil];

while([results next])
{
    if([results unsignedLongLongIntForColumnIndex:0]>0){
        updateOperation=TRUE;
    }
}
[results close];

if(updateOperation){

    [db executeUpdate:@"update `items` set OSXsize=?,OSXOsize=?, OSXDate=?, UUID=?,sourceFile=?,tombStone=0,SandBoxBookMark=?,songname=?,albumartist=? where key=?",
     size,originalSize, convertedDate,UUID,sourcePath,bookmark,fileName,albumArtist,[keyPath lowercaseString] , nil];
}
else
{
    [db executeUpdate:@"insert into `items`(key,filepath, OSXsize, OSXOsize, OSXdate,UUID,sourceFile,tombStone,SandBoxBookMark,songname,albumartist) values(?,?,?,?,?,?,?,0,?,?,?)",
     [keyPath lowercaseString], dapPath, size,originalSize, convertedDate,UUID,sourcePath,bookmark,fileName,albumArtist, nil];
}

I open the database once, however, I am seeing 4725+file handles attached as the app progresses in activity monitor:

/Users/userA/Library/Containers/com.map-pin.Dapper/Data/Library/Application Support/com.map-pin.Dapper/dapperright.sqlite
15
16
...
4724
4725

Solution

  • Is this in a multithread app and do you create and open a db instance every time you update/insert a row? Do you have [db close]; somewhere?

    Have you tested FMDatabaseQueue?