Swift词典需要很长时间才能编译

时间:2017-03-09 18:12:17

标签: swift

我有一个Realm闭包,用于更新已更改的行:

try realm.write {

                  realm.create(Product.self, value: ["itemgroup": item.itemgroup,
                                                     "itembrand": item.itembrand,
                                                     "itemtype": item.itemtype,
                                                     "itemsubtype": item.itemsubtype,
                                                     "basedescription": item.basedescription,
                                                     "info": item.info,
                                                     "upc": item.upc,
                                                     "upc2": item.upc2,
                                                     "upc3": item.upc3,
                                                     "upc4": item.upc4,
                                                     "upc5": item.upc5,
                                                     "baseprice": item.baseprice,
                                                     "proprice": item.proprice,
                                                     "retailprice": item.retailprice,
                                                     "stdprice": item.stdprice,
                                                     "caseqty": item.caseqty,
                                                     "spord": item.spord,
                                                     "category": item.category,
                                                     "categorycode": item.categorycode,
                                                     "allowinbc": item.allowinbc,
                                                     "allowinab": item.allowinab], update: true)
                }

然而,它需要花费近10分钟的时间来编译!

这是我的模型类:

class Product: Object {

    dynamic var itemno: String = ""
    dynamic var itemgroup: String = ""
    dynamic var itembrand: String = ""
    dynamic var itemtype: String = ""
    dynamic var itemsubtype: String = ""
    dynamic var basedescription: String = ""
    dynamic var info: String = ""
    dynamic var upc: String = ""
    dynamic var upc2: String = ""
    dynamic var upc3: String = ""
    dynamic var upc4: String = ""
    dynamic var upc5: String = ""
    dynamic var baseprice: Double = 0.00
    dynamic var proprice: Double = 0.00
    dynamic var retailprice: Double = 0.00
    dynamic var stdprice: Double = 0.00
    dynamic var caseqty: Int = 0
    dynamic var spord: String = ""
    dynamic var category: String = ""
    dynamic var categorycode: String = ""
    dynamic var allowinbc: String = ""
    dynamic var allowinab: String = ""

    override class func primaryKey() -> String {
        return "itemno"
    }

    convenience init(itemno: String, itemgroup: String, itembrand: String, itemtype: String, itemsubtype: String, basedescription: String, info: String, upc: String, upc2: String, upc3: String, upc4: String, upc5: String, baseprice: Double, proprice: Double, retailprice: Double, stdprice: Double, caseqty: Int, spord: String, category: String, categorycode: String, allowinbc: String, allowinab: String) {

        self.init()
        self.itemno = itemno
        self.itemgroup = itemgroup
        self.itembrand = itembrand
        self.itemtype = itemtype
        self.itemsubtype = itemsubtype
        self.basedescription = basedescription
        self.info = info
        self.upc = upc
        self.upc2 = upc2
        self.upc3 = upc3
        self.upc4 = upc4
        self.upc5 = upc5
        self.baseprice = baseprice
        self.proprice = proprice
        self.retailprice = retailprice
        self.stdprice = stdprice
        self.caseqty = caseqty
        self.spord = spord
        self.category = category
        self.categorycode = categorycode
        self.allowinbc = allowinbc
        self.allowinab = allowinab
    }

}

我可以对我的代码做些什么来加快编译速度?

感谢。

4 个答案:

答案 0 :(得分:2)

真的不是最好的'回答这里,因为所有答案都非常有用。编译大型Swift字典的最快方法是不要让编译器推断 任何 的类型。这意味着您需要定义字典中每个项目的数据类型(包括字典本身)。

我的字典编译的最快方式是:

try realm.write {

              let dict: [String: Any] = ["itemgroup": item.itemgroup as String,
                                         "itembrand": item.itembrand as String,
                                         "itemtype": item.itemtype as String,
                                         "itemsubtype": item.itemsubtype as String,
                                         "basedescription": item.basedescription as String,
                                         "info": item.info as String,
                                         "upc": item.upc as String,
                                         "upc2": item.upc2 as String,
                                         "upc3": item.upc3 as String,
                                         "upc4": item.upc4 as String,
                                         "upc5": item.upc5 as String,
                                         "baseprice": item.baseprice as Double,
                                         "proprice": item.proprice as Double,
                                         "retailprice": item.retailprice as Double,
                                         "stdprice": item.stdprice as Double,
                                         "caseqty": item.caseqty as Int,
                                         "spord": item.spord as String,
                                         "category": item.category as String,
                                         "categorycode": item.categorycode,
                                         "allowinbc": item.allowinbc as String,
                                         "allowinab": item.allowinab as String]

              realm.create(Product.self, value: dict, update: true)
            }

答案 1 :(得分:2)

我在创建大型词典时遇到过这个错误。一种选择是通过多个步骤创建并改变字典:

var dict: [String: Any] = ["itemgroup": item.itemgroup]
dict["itembrand"] = item.itembrand
// Populate rest of dictionary as above

try realm.write {
    realm.create(Product.self, value: dict)
}

答案 2 :(得分:1)

在Swift中编译速度慢的问题有99%与类型推断有关。这在复杂的表达式中最为明显(当编译器必须搜索太多可能性时)或从不同类型创建大型字典时。

作为一种解决方法,您始终可以通过指定预期类型来关闭类型推断,例如:

let dictionary: [String: Any] = [
   "itemgroup": item.itemgroup,
   ...
]
realm.create(Product.self, value: dictionary)

答案 3 :(得分:0)

这里的问题不在于Realm;这是Swift编译器。它很难编译大型词典。由于item已经是Product类型,您可以直接将其添加到领域(但您说它是常量,因此这可能会对您的代码进行一些结构性更改)。

另一种选择是用以下代码替换写块的主体:

let p = Product(value: item)
realm.add(p)