firebase updateChildValues需要很长时间才能在Swift中编译

时间:2016-11-11 22:30:00

标签: swift xcode firebase firebase-realtime-database

我有一个函数可以更新不允许我的程序编译的订单的大量记录。它开始构建,并且在报告导航器中它会停留在此功能上。如果我删除某些部分它最终会构建并运行正常,但使用所有代码将使我的MacBook pro耗尽内存。我可以做些什么来加速updateChildValues或以更有效的方式做我正在做的事情?

func updateOrder(_ ref: FIRDatabaseReference){
    //update the order data
    ref.updateChildValues( [
        "seq": seq,
        "shiftSeq": self.shiftSeq,
        "serviceType": serviceTypeString,
        "status": "\(self.status.rawValue)",
        "promisedTime": self.promisedTime,
        "customer": true,
        "customerFirstName": self.customerFirstName.lowercased(),
        "customerLastName": self.customerLastName.lowercased(),
        "customerPhone": self.customerPhone,
        "customerAddress": self.customerAddress.lowercased(),
        "items": true,
        "discounts": true,
        "payments": true,
        "working": self.working,
        "total": self.total,
        "tax": self.tax,
        "tip": self.tip,
        "manualTip": self.manualTip,
        "serviceFees": true,
        "isOnline": self.isOnline,
        "owner": owner,
        "driver": driver,
        "settled": self.settled,
        "printed": self.printed,
        "voided": self.voided,
        "created": self.createdTime,
        "lastUpdated": FIRServerValue.timestamp()
    ])

    //set items
    for item in self.items{
        let itemRef = ref.child("items").childByAutoId()

        //set item
        itemRef.updateChildValues([
            "parentCategoryId": item.parentCategory.id,
            "itemId": item.item.id,
            "name": item.item.name,
            "qty": item.qty,
            "taxRate": item.taxRate,
            "discountAmount": item.discountAmount,
            "notes": item.notes,
            "price": item.price,
            "options": true,
            "removedOptions": true,
            "discounts": true
        ])

        //set options
        for option in item.options{
            let optionRef = itemRef.child("options").child(option.item.id)
            optionRef.updateChildValues([
                "name" : option.item.name,
                "group": option.group.id,
                "qty": option.qty,
                "price": option.price
            ])
        }

        //set removed options
        for option in item.removedOptions{
            let optionRef = itemRef.child("removedOptions").child(option.item.id)
            optionRef.updateChildValues([
                "name" : option.item.name ,
                "group": option.group.id,
                "qty": option.qty,
                "price": option.price
            ])
        }

        //set discounts
        var manualCount: Int = 0
        for discount in item.discounts{
            if let discountId = discount.id{
                let discountRef = itemRef.child("discounts").child(discountId)
                discountRef.setValue(true, withCompletionBlock: {(error, success) in })
            }else if discount.type == DiscountType.manual{
                manualCount += 1
                let discountRef = itemRef.child("discounts").child("manual\(manualCount)")
                discountRef.setValue(true)
            }
        }
    }

    //set discounts
    for discount in self.discounts {
        let discountRef = ref.child("discounts").childByAutoId()

        var discountId: String? = discount.id
        if discountId == nil {
            discountId = ""
        }

        discountRef.updateChildValues([
            "id": discountId,
            "name" : discount.name,
            "code" : discount.code,
            "amountType" : discount.amountType,
            "amount" : discount.amount,
            "type" : discount.type,
            "limit" : discount.limit,
            "combined" : discount.combined,
            "serviceType" : discount.serviceType,
            "minOrder" : discount.minOrder,
            "minRequired" : discount.minRequired,
            "requiredDiscountCount" : discount.requiredDiscountCount,
            "startDate" : discount.startDate,
            "endDate" : discount.endDate,
            "startTime" : discount.startTime,
            "endTime" : discount.endTime,
            "mon" : discount.mon,
            "tue" : discount.tue,
            "wed" : discount.wed,
            "thu" : discount.thu,
            "fri" : discount.fri,
            "sat" : discount.sat,
            "sun" : discount.sun,
            "discountItems" : true,
            "discountRequiredItems" : true
        ])
    }
}

0 个答案:

没有答案