【发布时间】:2015-08-16 02:35:30
【问题描述】:
我有一个在模拟器中运行良好的应用程序,但是当我在设备上运行它时,应用程序运行缓慢并且偶尔会崩溃。当点击表格视图单元格并转换到新视图时,会特别出现此问题。当点击一个单元格时会有一两秒的延迟,那么新视图将以缓慢的延迟方式推送。这也是它偶尔会崩溃的地方。
该应用程序在设备上运行良好,直到最近对代码进行了一些更改,我认为这就是问题所在。我在下面添加了包含可疑代码的整个 viewDidLoad(在代码中表示)。
我注意到的另一件事是,调试导航器中的 CPU 使用率达到 130% 左右,而应用程序在 sim 卡中运行时会在设备上出现问题。
override func viewDidLoad() {
super.viewDidLoad()
// screen size for collection view cell
screenSize = UIScreen.mainScreen().bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
dimView.alpha = 0
openingHoursView.alpha = 0
resDescriptionLabel.layer.borderColor = UIColor.blackColor().CGColor
resDescriptionLabel.layer.borderWidth = 2
resDescriptionLabel.layer.cornerRadius = 4
//add logo to nav bar
let navBarLogo: UIImageView = UIImageView(frame: CGRectMake(0, 0, 120, 36))
navBarLogo.image = UIImage(named: "logo")
self.navigationItem.titleView = navBarLogo
self.resImage.image = UIImage(named: "placeholder")
if let checkedUrl = NSURL(string: "http://staging.api.cheapeat.com.au/restaurants/\(self.venueID)/photo") {
downloadImage(checkedUrl)
}
self.resName.text = " " + self.venueName + " "
self.resAdd.text = " " + self.venueAdd + " "
self.resPhone.text = "\(self.venuePH)"
self.resPhoneNumber.text = self.venuePH
self.resWebText.text = "\(self.venueWeb)"
///////////////////// new code starts here //////////////
var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Justified
paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping
var attributedString = NSAttributedString(string: self.venueInfo,
attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSBaselineOffsetAttributeName: NSNumber(float: 0)
])
self.resDescriptionLabel.attributedText = attributedString
aboutVenueLabelWidthConstraint.constant = screenWidth - 16
resDescriptionLabel.edgeInsets.left = 10
resDescriptionLabel.edgeInsets.top = 10
resDescriptionLabel.edgeInsets.right = 10
resDescriptionLabel.edgeInsets.bottom = 10
resDescriptionLabel.layoutIfNeeded()
backgroundImageHeightConstraint.constant = resDescriptionLabel.bounds.height + 130
// set content view height i.e. scrollable area
// (dynamic height of label + combined height of all other content and contraints)
contentViewHeightConstraint.constant = resDescriptionLabel.bounds.height + 790
/////////////////// new code ends here ///////////////
self.displayMap(self.venueLat, lng: self.venueLng)
self.getArrayForCollection()
self.getArrayValues()
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
self.getDataForNextTable()
})
mon.text = timeFormatting(openingHours, day: "Monday")
tue.text = timeFormatting(openingHours, day: "Tuesday")
wed.text = timeFormatting(openingHours, day: "Wednesday")
thu.text = timeFormatting(openingHours, day: "Thursday")
fri.text = timeFormatting(openingHours, day: "Friday")
sat.text = timeFormatting(openingHours, day: "Saturday")
sun.text = timeFormatting(openingHours, day: "Sunday")
}
【问题讨论】:
-
尝试一一去除约束
-
另外,请确保 URL 处于活动状态,我去了它的根目录并显示它现在没有提供服务,但可能是他们以这种方式设置它以防止黑客攻击或其他什么。另外,我不确定你的函数“downloadImage(checkedUrl)”是做什么的,看看这个函数做了什么来确定这是否是罪魁祸首会很有帮助
-
使用名为“Time Profiler”的 Xcode 工具 Instruments 来检测代码中的问题位置。
标签: ios performance swift uitableview crash