memory usage increases dramatically after each FMDB query

时间秒杀一切 提交于 2019-12-11 08:02:05

问题


Below is my source code, every time I execute the function, the memory usage increases dramatically. Please help to point out what is the problem.

func loadfontsFromDatabase(code:String)->[String] {
    let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0] as AnyObject
    let databasePath = documentsPath.appending("/bsmcoding.sqlite")
    let contactDB = FMDatabase(path: databasePath as String)
    var c:[String]=[]

    let querySQL = "SELECT FONT FROM BSMCODE WHERE BSMCODE.CODE = '\(code)' ORDER BY NO DESC"

    NSLog("query:\(querySQL)")

    let results:FMResultSet? = Constants.contactDB?.executeQuery(querySQL, withArgumentsIn: nil)

    while (results?.next())! {
        c.append((results?.string(forColumn: "FONT"))!)
    }

    results?.close()

    return c
}

回答1:


There's nothing here that would account for any substantial memory loss. I would suggest using the "Debug Memory Graph" feature in Xcode 8 to identify what objects are being created and not being released, but I suspect the problem rests elsewhere in your code. Or use Instruments to track it down what's leaking and debug from there. See https://stackoverflow.com/a/30993476/1271826.

There are unrelated issues here, though:

  1. You are creating local contactDB, but you never open it and you never use it. It will be released when the routine exits, but it's completely unnecessary if you're going to use Constants.contactDB, anyway.

  2. I'd advise against using string interpolation when building your SQL. Use ? placeholder and pass the code in as a parameter. This is much safer, in case the code ever contained something that couldn't be represented in SQL statement. (This is especially true if the code was supplied by the user, in which case you'd be susceptible to SQL injection attacks or innocent input errors that could lead to crashes.)

    For example, you could do something like:

    func loadfontsFromDatabase(code: String) -> [String] {
        var c = [String]()
    
        let querySQL = "SELECT FONT FROM BSMCODE WHERE BSMCODE.CODE = ? ORDER BY NO DESC"
    
        let results = try! Constants.contactDB!.executeQuery(querySQL, values: [code])
    
        while results.next() {
            c.append((results.string(forColumn: "FONT"))!)
        }
    
        return c
    }
    

    If you don't like the forced unwrapping, you can do optional unwrapping if you want, but personally I'd rather know immediately when debugging during the development phase if there's some logic mistake (e.g. the contactDB wasn't open, the SQL is incorrect, etc.). But you can do optional binding and add the necessary guard statements if you want. But don't just do optional binding and silently return a value suggesting that everything is copacetic, leaving you with a debugging challenge of tracking down the problem if you don't get what you expected.

    But the key point is to avoid inserting values into your SQL directly. Use ? placeholders.



来源:https://stackoverflow.com/questions/42982930/memory-usage-increases-dramatically-after-each-fmdb-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!