【问题标题】:Inserting three thousand string data in database with FMDB using isTransaction使用 isTransaction 在 FMDB 数据库中插入三千个字符串数据
【发布时间】:2017-11-20 19:12:06
【问题描述】:

我试图在首次启动时将大约 3000 个字符串数据插入到我的应用程序中。我遇到的问题是插入部分花费了大约 15-20 秒的时间太长。我查找了提高 isTransaction 方法出现速度的方法,但我不知道如何使用它。我在网上尝试了很多例子,但似乎不太适合我的情况。非常感谢您的帮助。

    func insertWordData() {

    if openDatabase() {


        if let path = Bundle.main.path(forResource: "messagesToRead", ofType: "json") {
        var query = ""
        do {
            let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)

            let jsonObj = JSON(data: data)
            if jsonObj != JSON.null {
                for (_, jsonObj) in jsonObj {

                    let WordString = jsonObj["FIELD1"]
                    let WordDefinition = jsonObj["FIELD2"]

                    query += "insert into words (\(field_WordID), \(field_WordString), \(field_WordDefinition)) values (null, '\(WordString)', '\(WordDefinition)');"
                }
            }
            if !database.executeStatements(query) {
                print("Failed to insert initial data into the database.")
                print(database.lastError(), database.lastErrorMessage())
            }
            else {
                //print(words)
            }
        } catch let error {
            print(error.localizedDescription)
        }
    } else {
        print("Invalid filename/path.")
        }

        database.close()
    }
}

【问题讨论】:

    标签: ios swift3 fmdb


    【解决方案1】:

    没关系,问题已经解决,我所要做的就是在 for 循环之前添加 database.beginTransaction(),在 database.close() 之前添加 database.commit()。速度从 15-20 秒变为 0.5 秒

    【讨论】:

      猜你喜欢
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 2011-08-02
      • 2013-04-15
      • 1970-01-01
      • 2016-10-04
      • 1970-01-01
      相关资源
      最近更新 更多