Lawrence Gimenez

OnlineJobs for iOS (v4.5.2) Bug

Last week, it was one of those times where you know something went wrong when suddenly you get multiple Slack notifications. I received a Slack notification for every minute, I had to mute the channel.

When I open the Bugsnag dashboard, I could see it was affecting hundreds of users. As of this moment, 154 users. Yikes!

What took me so long was that Bugsnag wasn't able to capture all the other threads. I had to go back to Xcode's Organizer Reports. I've been recently relying more on Xcode's crash reports because they tend to be more detailed and well integrated with Xcode.

The bug was somewhere in this code block. This is for parsing an HTML String and it was crashing right on viewDidLoad() function.

extension Data {
    var html2AttributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: self, options: [
                .documentType: NSAttributedString.DocumentType.html,
                .characterEncoding: String.Encoding.utf8.rawValue
            ], documentAttributes: nil)
        } catch {
            return  nil
        }
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}
extension String {
    var html2AttributedString: NSAttributedString? {
        return Data(utf8).html2AttributedString
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}

Then I realized since in this ViewController I did not present this as full screen but in a modal kind of way, somehow it was affecting the lifecycle. The fix I did was to transfer the HTML parsing to viewDidAppear() function to give time to parse the HTML. ViewDidAppear is the callback that tells the ViewController that the views are added to the view hierarchy.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    lastMessageLabel.text = message?.body?.html2String
}

That was it, the simple fix. Sorry guys, please download the latest OnlineJobs update v4.5.3.

If you like to receive daily mobile development posts, you can subscribe below. I’ll be posting frequently.