The iOS app works fine in the Xcode simulator, but it runs slowly and crashes on the device

I have an application that works fine in the simulator, however, when I run it on the device, the application runs slowly and crashes from time to time. This problem occurs when you click on a tableview cell and switch to a new view. When you click on a cell, a second or two delays occur, then the new view will slowly move forward. This is also the place where it will sometimes break.

The application worked fine on the device until recently, adding some changes to the code in which I think the problem lies. I added the entire viewDidLoad file containing the suspect code (specified in the code).

Another thing that I noticed is that CPU usage in the debug navigator is about 130%, and the application runs in sim at the point where the problem occurs on the device.

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 ie 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") } 
+4
source share
2 answers

I am sure that the lag and failures come from this synchronously looking call to downloadImage(checkedUrl) , but only profiling through Xcode Instruments will tell you for sure

Not only do you depend on a potentially slow network to get an image in your main stream, but you don’t know how big this image will be (if it is a high resolution image, and you already have many other high-rez UIImages uploaded to other cells, you will most likely exit the application on your low memory device.)

This will not be a trivial fix in which I can just enter the code to solve it, but I recommend making a few changes:

one)

Use an image cache that loads images asynchronously

eg. SDWebImage

2)

Display reduced (and less memory intensive) thumbnails in a table view, rather than full-size images.

eg. maybe through this method

0
source

I found the criminal, it turned out that he was not in the code after all. I set some background images in scrollView, which were in huge resolutions (5000x5000). They completely chewed on the memory (in the debugger there were 400 mb). I resized images, the problem is resolved.

0
source

All Articles