实例方法需要很长时间进行类型检查

时间:2018-08-13 07:30:06

标签: swift compiler-warnings

我有一个很大的项目,需要很长时间才能编译。当我检查方法的编译时间时。我发现例如这种方法通常需要200毫秒以上的时间来编译。我该怎么做才能改善这一点?

func processSnapshot(snapshot:DataSnapshot) {
    self.numberOfUsers.text = "#Total DB (users): \(snapshot.childrenCount.description)"


    for child in snapshot.children {
        if let snapshot = child as? DataSnapshot,
            let userProfile = UserProfile(snapshot: snapshot) {
            if userProfile.picUrl == "" {
                numberOfUsersWithOutPicUrl += 1
            }

            let todayLong = "\(Date())".prefix(10)
            if userProfile.createdOnLong.prefix(10) == todayLong {
                profileWizardsStartedTotday += 1
                if userProfile.weight != "" {
                    profileWizardsCompletedTotday += 1
                }
            }

            profiles.append(userProfile)
        }
    }

    let numberOfUsersWithOutPicUrlPercent = round(Double(numberOfUsersWithOutPicUrl)*100/Double(numberOfRealUsers))

    let sizeOfSnapshotValueInKB:Int = String(describing: snapshot.value).count / 1024
    self.userThatAbortedOnBoarding.text = numberOfUsersWithOutPicUrl.description
    self.userThatAbortedOnBoardingPercent.text = "\(numberOfUsersWithOutPicUrlPercent)%"
    self.userThatStartedOnBoardingToday.text = profileWizardsStartedTotday.description
    self.userThatCompletedOnBoardingToday.text = profileWizardsCompletedTotday.description
    self.sizeOfSnapshot.text = "\(sizeOfSnapshotValueInKB.description)KB"

}

我的UserProfile初始化程序如下:

init?(snapshot: DataSnapshot) {
    print("\(snapshot.key) - \(String(describing: snapshot.value))")
    guard
        let value = snapshot.value as? [String: AnyObject],
        let uid = value["uid"] as? String

        else {
            print("Warning: Userprofile has no UID")
            return nil
    }

    self.uid = uid

    self.email      = value["email"] as? String ?? ""
    self.picUrl     = value["picurl"] as? String ?? ""        
    self.picUrl2    = value["picurl2"] as? String ?? ""
    self.picUrl3    = value["picurl3"] as? String ?? ""
    self.picUrl4    = value["picurl4"] as? String ?? ""
    self.picUrl5    = value["picurl5"] as? String ?? ""
    self.picUrl6    = value["picurl6"] as? String ?? ""
    self.city       = value["city"] as? String ?? ""
    self.displayName = value["displayName"] as? String ?? "anonymous"
}

0 个答案:

没有答案