Too many crashes about scroll to bottom - ios

I am getting too many crashes from my users for scroll to bottom. What can be cause to this problem?
I am scrolling to bottom my tableview when keyboard appears. And this causes to crash. My code:
func keyboardWillShow(notification: NSNotification) {
var info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.2) {
self.bottomCons.constant = keyboardFrame.size.height
self.view.layoutIfNeeded()
}
let numberOfRows = tableView.numberOfRowsInSection(0)
if numberOfRows != 0 {
do {
try tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: numberOfRows-1, inSection: 0), atScrollPosition: .Bottom, animated: false) //This line is crashing
}catch {
print("error")
}
}
}
Crash log:
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x000000010013e018
Termination Signal: Trace/BPT trap: 5
Termination Reason: Namespace SIGNAL, Code 0x5
Terminating Process: exc handler [0]
Triggered by Thread: 0
Thread 0 name:
Thread 0 Crashed:
0 Ribony 0x000000010013e018 specialized ChatViewController.tableView(UITableView, cellForRowAtIndexPath : NSIndexPath) -> UITableViewCell + 1904 (ChatViewController.swift:0)
1 Ribony 0x0000000100135890 #objc ChatViewController.tableView(UITableView, cellForRowAtIndexPath : NSIndexPath) -> UITableViewCell + 76 (ChatViewController.swift:0)
2 UIKit 0x000000018e315b58 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 716 (UITableView.m:10665)
3 UIKit 0x000000018e315e00 -[UITableView _createPreparedCellForRowAtIndexPath:willDisplay:] + 88 (UITableView.m:10717)
4 UIKit 0x000000018e31b4a8 -[UITableView _heightForRowAtIndexPath:] + 256 (UITableView.m:12615)
5 UIKit 0x000000018e1127c4 -[UISectionRowData heightForRow:inSection:canGuess:] + 280 (UITableViewRowData.m:288)
6 UIKit 0x000000018e4a413c -[UITableViewRowData ensureHeightsFaultedInForIndexPath:availHeight:edgeInset:scrollPosition:] + 1484 (UITableViewRowData.m:2418)
7 UIKit 0x000000018e1ef4f8 -[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:] + 1792 (UITableView.m:5544)
8 UIKit 0x000000018e1eecb0 -[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:] + 52 (UITableView.m:5644)
9 Ribony 0x000000010013ed00 specialized ChatViewController.keyboardWillShow(NSNotification) -> () + 800 (ChatViewController.swift:550)
10 Ribony 0x0000000100136e98 #objc ChatViewController.keyboardWillShow(NSNotification) -> () + 56 (ChatViewController.swift:0)
11 CoreFoundation 0x00000001880b6b10 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20 (CFNotificationCenter.c:650)
12 CoreFoundation 0x00000001880b6214 _CFXRegistrationPost + 400 (CFNotificationCenter.c:164)
13 CoreFoundation 0x00000001880b5f90 ___CFXNotificationPost_block_invoke + 60 (CFNotificationCenter.c:1031)
14 CoreFoundation 0x0000000188125b8c -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1504 (CFXNotificationRegistrar.m:163)
15 CoreFoundation 0x0000000187ff7e64 _CFXNotificationPost + 376 (CFNotificationCenter.c:1028)
16 Foundation 0x0000000188b2ce0c -[NSNotificationCenter postNotificationName:object:userInfo:] + 68 (NSNotification.m:482)
17 UIKit 0x000000018e9a6b40 -[UIInputWindowController postStartNotifications:withInfo:] + 400 (UIInputWindowController.m:1375)
18 UIKit 0x000000018e9a8cf0 __77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke.907 + 388 (UIInputWindowController.m:1816)
19 UIKit 0x000000018e0080f0 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 636 (UIView.m:11811)
20 UIKit 0x000000018e0d952c +[UIView(UIViewAnimationWithBlocks) _animateWithDuration:delay:options:animations:start:completion:] + 128 (UIView.m:11865)
21 UIKit 0x000000018e9a876c -[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:] + 1368 (UIInputWindowController.m:1877)
22 UIKit 0x000000018e9af268 -[UIInputWindowController setInputViewSet:] + 1444 (UIInputWindowController.m:2923)
23 UIKit 0x000000018e9a7e38 -[UIInputWindowController performOperations:withAnimationStyle:] + 56 (UIInputWindowController.m:1674)
24 UIKit 0x000000018e099278 -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1276 (UIPeripheralHost.m:4647)
25 UIKit 0x000000018dff8a78 -[UIResponder(UIResponderInputViewAdditions) reloadInputViews] + 80 (UIResponder.m:1107)
26 UIKit 0x000000018e056b4c -[UIResponder becomeFirstResponder] + 600 (UIResponder.m:272)
27 UIKit 0x000000018e056ebc -[UIView(Hierarchy) becomeFirstResponder] + 148 (UIView.m:9419)
28 UIKit 0x000000018e0d90b4 -[UITextField becomeFirstResponder] + 60 (UITextField.m:1396)
29 UIKit 0x000000018e180128 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 192 (UITextInteractionAssistant.m:1160)
30 UIKit 0x000000018e17f630 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] + 3024 (UITextInteractionAssistant.m:1770)
31 UIKit 0x000000018e59af80 -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 64 (UIGestureRecognizer.m:103)
32 UIKit 0x000000018e59e688 _UIGestureRecognizerSendTargetActions + 124 (UIGestureRecognizer.m:986)
33 UIKit 0x000000018e16573c _UIGestureRecognizerSendActions + 260 (UIGestureRecognizer.m:1020)
34 UIKit 0x000000018e0040f0 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 764 (UIGestureRecognizer.m:1067)
35 UIKit 0x000000018e58e680 _UIGestureEnvironmentUpdate + 1100 (UIGestureEnvironment.m:148)
36 UIKit 0x000000018e58e1e0 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 408 (UIGestureEnvironment.m:1181)
37 UIKit 0x000000018e58d49c -[UIGestureEnvironment _updateGesturesForEvent:window:] + 268 (UIGestureEnvironment.m:1090)
38 UIKit 0x000000018e00230c -[UIWindow sendEvent:] + 2960 (UIWindow.m:2289)
39 UIKit 0x000000018dfd2da0 -[UIApplication sendEvent:] + 340 (UIApplication.m:10729)
40 UIKit 0x000000018e7bc75c __dispatchPreprocessedEventFromEventQueue + 2736 (UIEventDispatcher.m:1430)
41 UIKit 0x000000018e7b6130 __handleEventQueue + 784 (UIEventDispatcher.m:1626)
42 CoreFoundation 0x00000001880cab5c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24 (CFRunLoop.c:1943)
43 CoreFoundation 0x00000001880ca4a4 __CFRunLoopDoSources0 + 524 (CFRunLoop.c:1989)
44 CoreFoundation 0x00000001880c80a4 __CFRunLoopRun + 804 (CFRunLoop.c:2821)
45 CoreFoundation 0x0000000187ff62b8 CFRunLoopRunSpecific + 444 (CFRunLoop.c:3113)
46 GraphicsServices 0x0000000189aaa198 GSEventRunModal + 180 (GSEvent.c:2245)
47 UIKit 0x000000018e03d7fc -[UIApplication _run] + 684 (UIApplication.m:2650)
48 UIKit 0x000000018e038534 UIApplicationMain + 208 (UIApplication.m:4092)
49 Ribony 0x00000001000ce070 main + 120 (AppDelegate.swift:15)
50 libdyld.dylib 0x0000000186fd95b8 start + 4
As you can see i am using this in a try catch block. It should not crash. How is this possible?

Try this code
func keyboardWillShow(notification: NSNotification) {
var info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.2) {
self.bottomCons.constant = keyboardFrame.size.height
self.view.layoutIfNeeded()
}
tableViewScrollToBottom(true)
}
func tableViewScrollToBottom(animated: Bool) {
let delay = 0.2 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
let numberOfSections = self.tableView.numberOfSections()
let numberOfRows = self.tableView.numberOfRowsInSection(numberOfSections-1)
if numberOfRows > 0 {
let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
}
})
}

Related

specialized UIViewController calling some method crashing iOS Swift

I am unable to replicate this issue, how to replicate the following fabric crash ?
Is there a way to open this crash in xcode (Importing crash into xcode)
Crashed: com.apple.main-thread
0 ABCC 0x10092d0c4 specialized ABCViewController.signInBtnPressed(UIButton) -> ()(ABCViewController.swift:178)
1 ABCC 0x10092a520 #objc ABCViewController.unwind(UIStoryboardSegue) -> () + 3152641510
2 UIKit 0x18aced64c -[UIApplication sendAction:to:from:forEvent:] + 86
3 UIKit 0x18ae0e870 -[UIControl sendAction:to:forEvent:] + 52
4 UIKit 0x18acf3700 -[UIControl _sendActionsForEvents:withEvent:] + 120
5 UIKit 0x18ae291a8 -[UIControl touchesEnded:withEvent:] + 492
6 UIKit 0x18b2ceee8 _UIGestureEnvironmentSortAndSendDelayedTouches + 4340
7 UIKit 0x18b2cbc60 _UIGestureEnvironmentUpdate + 1236
8 UIKit 0x18ad664d8 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 404
9 UIKit 0x18ad66010 -[UIGestureEnvironment _updateGesturesForEvent:window:] + 276
10 UIKit 0x18ad65874 -[UIWindow sendEvent:] + 3132
11 UIKit 0x18ad641d0 -[UIApplication sendEvent:] + 340
12 UIKit 0x18b545d1c __dispatchPreprocessedEventFromEventQueue + 2340
13 UIKit 0x18b5482c8 __handleEventQueueInternal + 4744
14 CoreFoundation 0x180f27404 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
15 CoreFoundation 0x180f26c2c __CFRunLoopDoSources0 + 276
16 CoreFoundation 0x180f2479c __CFRunLoopRun + 1204
17 CoreFoundation 0x180e44da8 CFRunLoopRunSpecific + 552
18 GraphicsServices 0x182e2a020 GSEventRunModal + 100
19 UIKit 0x18ae64758 UIApplicationMain + 236
20 ABCC 0x1007b28c4 main (main.m:20)
21 libdyld.dylib 0x1808d5fc0 start + 4
Here is the code for the action that is crashing.
#IBAction func signInBtnPressed(_ sender: UIButton) {
sender.isUserInteractionEnabled = false
guard let employees = UserService.shared.employees, let name = employees.names?[sender.tag] else {
sender.isUserInteractionEnabled = true // crashing here
return
}
// perform task
sender.isUserInteractionEnabled = true
}

NSAttributedString crash when converting HTML to Attrubuted String

I have a situation where try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) crashes the app.
In the console it says:
Assertion failure in void _prepareForCAFlush(UIApplication *__strong)()
The function is called in an extension String.
When I 'po' values in the console:
(lldb) po self
<p>Obfuscated string\n</p>
(lldb) po data`
450 bytes
count : 450
pointer : 0x00007fd283d75630
pointerValue : 140542131787312
(lldb) po NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
Obfuscated string
{
NSColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSFont = "<UICTFont: 0x7fd283c47330> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 15, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 19/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSStrokeWidth = 0;
}
The string exists in a label, that is displayed in a UITableViewCell.
The function crashes in a specific case, when the "keyboard frame changes".
The Table Cells are being drawn again by iOS and this causes to redraw their content.
The crash happens on the main thread, so the function is being called on the main thread.
What could be the cause of this crash and how could I possibly resolve it?
EDIT: Table view cell code
UITableViewDataSource to generate the cell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellID = cellIdentifier(for: message)
let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as! MessageTableViewCell
cell.message = message
cell.setOutletValues()
cell.selectionStyle = .none
cell.delegate = self
return cell
}
Implementaion of the Cell:
class MessageTableViewCell: UITableViewCell {
var message: Message!
// Message Body View
#IBOutlet weak var messageBodyView: ZeroPaddingTextView!
func setOutletValues() {
setMessageBodyOutletValues()
}
internal func setMessageBodyOutletValues() {
if let body = message.body {
messageBodyView.attributedText = body.htmlToPlainAttributedString()
}
}
}
And the function to create a plain attributed string, it does a few additions tothe HTML for styling (this is due to the content that comes in through the API, it needs some additions).
func htmlToPlainAttributedString() -> NSAttributedString {
let contentString = replacingOccurrences(of: "\n", with: "<br>")
let styleSheet = "body {font-family: sans-serif; font-size: 15px; color: #000000;}\n a {text-decoration: none;}\n"
let body = "<body>\(contentString)</body>"
let html = "<html><head><style type=\"text/css\">\(styleSheet)</style></head>\(body)</html>"
if let data = html.data(using: String.Encoding.unicode, allowLossyConversion: true) {
do {
return try NSAttributedString(data: data, options: [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
} catch {
return NSAttributedString()
}
}
return NSAttributedString()
}
Console crash output:
2017-09-05 22:37:14.692 trustedfamily-ios[70389:10080542] *** Assertion failure in void _prepareForCAFlush(UIApplication *__strong)(), /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UIApplication.m:2395
2017-09-05 22:38:14.532 trustedfamily-ios[70389:10080542] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unexpected start state'
*** First throw call stack:
(
0 CoreFoundation 0x000000010452ab0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001035e8141 objc_exception_throw + 48
2 CoreFoundation 0x000000010452ecf2 +[NSException raise:format:arguments:] + 98
3 Foundation 0x00000001031b769b -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 165
4 UIKit 0x0000000105a31575 _prepareForCAFlush + 499
5 UIKit 0x0000000105a6346b _beforeCACommitHandler + 15
6 CoreFoundation 0x00000001044d0717 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
7 CoreFoundation 0x00000001044d0687 __CFRunLoopDoObservers + 391
8 CoreFoundation 0x00000001044b5038 CFRunLoopRunSpecific + 440
9 UIFoundation 0x000000010a103edc -[NSHTMLReader _loadUsingWebKit] + 1954
10 UIFoundation 0x000000010a10522a -[NSHTMLReader attributedString] + 22
11 UIFoundation 0x000000010a09ded6 _NSReadAttributedStringFromURLOrData + 8926
12 UIFoundation 0x000000010a09bb64 -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:] + 117
13 trustedfamily-ios 0x0000000101e9e6ad _TTOFE5UIKitCSo18NSAttributedStringcfzT4dataV10Foundation4Data7optionsGVs10DictionarySSP__18documentAttributesGSqGVs33AutoreleasingUnsafeMutablePointerGSqCSo12NSDictionary____S0_ + 173
14 trustedfamily-ios 0x0000000101e9d9d9 _TFE5UIKitCSo18NSAttributedStringCfzT4dataV10Foundation4Data7optionsGVs10DictionarySSP__18documentAttributesGSqGVs33AutoreleasingUnsafeMutablePointerGSqCSo12NSDictionary____S0_ + 89
15 trustedfamily-ios 0x0000000101e9d803 _TFE17trustedfamily_iosSS27htmlToPlainAttributedStringfT_CSo18NSAttributedString + 1459
16 trustedfamily-ios 0x00000001020152f9 _TFC17trustedfamily_ios20MessageTableViewCell26setMessageBodyOutletValuesfT_T_ + 601
17 trustedfamily-ios 0x0000000102013467 _TFC17trustedfamily_ios20MessageTableViewCell15setOutletValuesfT_T_ + 103
18 trustedfamily-ios 0x0000000101f9a22f _TFC17trustedfamily_ios32ConversationDetailViewController9tableViewfTCSo11UITableView12cellForRowAtV10Foundation9IndexPath_CSo15UITableViewCell + 1199
19 trustedfamily-ios 0x0000000101f9a4d7 _TToFC17trustedfamily_ios32ConversationDetailViewController9tableViewfTCSo11UITableView12cellForRowAtV10Foundation9IndexPath_CSo15UITableViewCell + 87
20 UIKit 0x0000000105ba4ab2 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 750
21 UIKit 0x0000000105ba4cf8 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
22 UIKit 0x0000000105b79639 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2845
23 UIKit 0x0000000105b774a4 -[UITableView _setNeedsVisibleCellsUpdate:withFrames:] + 201
24 UIKit 0x0000000105b9626a -[UITableView _rectChangedWithNewSize:oldSize:] + 1267
25 UIKit 0x0000000105b96a5c -[UITableView setBounds:] + 322
26 UIKit 0x0000000105adee73 -[UIView(Geometry) _applyISEngineLayoutValuesToBoundsOnly:] + 598
27 UIKit 0x0000000105adf15e -[UIView(Geometry) _resizeWithOldSuperviewSize:] + 125
28 UIKit 0x000000010648f0e9 -[UIScrollView(_UIOldConstraintBasedLayoutSupport) _resizeWithOldSuperviewSize:] + 46
29 CoreFoundation 0x00000001044bb652 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 114
30 CoreFoundation 0x00000001044bb56f -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 335
31 UIKit 0x0000000105addbcc -[UIView(Geometry) resizeSubviewsWithOldSize:] + 183
32 UIKit 0x00000001063fa16d -[UIView(AdditionalLayoutSupport) _is_layout] + 168
33 UIKit 0x0000000105aea0a6 -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 994
34 UIKit 0x0000000105afb55b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1268
35 QuartzCore 0x00000001057a4904 -[CALayer layoutSublayers] + 146
36 QuartzCore 0x0000000105798526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370
37 UIKit 0x0000000105ae9334 -[UIView(Hierarchy) layoutBelowIfNeeded] + 1108
38 trustedfamily-ios 0x0000000101fe3f4c _TFC17trustedfamily_ios32ConversationDetailViewController28changeBottomLayoutConstraintfT2toV12CoreGraphics7CGFloat_T_ + 284
39 trustedfamily-ios 0x0000000101fe3fac _TToFC17trustedfamily_ios32ConversationDetailViewController28changeBottomLayoutConstraintfT2toV12CoreGraphics7CGFloat_T_ + 44
40 trustedfamily-ios 0x0000000101fe3bf7 _TFFC17trustedfamily_ios32ConversationDetailViewController28changeBottomLayoutConstraintFT2toV12CoreGraphics7CGFloat8animatedSb8durationSd7optionsVSC22UIViewAnimationOptions_T_U_FT_T_ + 39
41 trustedfamily-ios 0x0000000101e572d7 _TTRXFo___XFdCb___ + 39
42 UIKit 0x0000000105af13da +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 572
43 UIKit 0x0000000105af18dd +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 99
44 trustedfamily-ios 0x0000000101fe3b7f _TFC17trustedfamily_ios32ConversationDetailViewController28changeBottomLayoutConstraintfT2toV12CoreGraphics7CGFloat8animatedSb8durationSd7optionsVSC22UIViewAnimationOptions_T_ + 943
45 trustedfamily-ios 0x0000000101fe3e1f _TToFC17trustedfamily_ios32ConversationDetailViewController28changeBottomLayoutConstraintfT2toV12CoreGraphics7CGFloat8animatedSb8durationSd7optionsVSC22UIViewAnimationOptions_T_ + 79
46 trustedfamily-ios 0x0000000101fe30e6 _TFC17trustedfamily_ios32ConversationDetailViewController23keyboardWillChangeFramefV10Foundation12NotificationT_ + 2278
47 trustedfamily-ios 0x0000000101fe3237 _TToFC17trustedfamily_ios32ConversationDetailViewController23keyboardWillChangeFramefV10Foundation12NotificationT_ + 71
48 CoreFoundation 0x00000001044c9c2c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
49 CoreFoundation 0x00000001044c9b29 _CFXRegistrationPost + 425
50 CoreFoundation 0x00000001044c9892 ___CFXNotificationPost_block_invoke + 50
51 CoreFoundation 0x000000010448d102 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1826
52 CoreFoundation 0x000000010448c261 _CFXNotificationPost + 673
53 Foundation 0x00000001030b6ca4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
54 UIKit 0x000000010649ce05 -[UIInputWindowController postStartNotifications:withInfo:] + 225
55 UIKit 0x000000010649f0af __77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke.871 + 381
56 UIKit 0x0000000105af13da +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 572
57 UIKit 0x0000000105af1853 +[UIView(UIViewAnimationWithBlocks) _animateWithDuration:delay:options:animations:start:completion:] + 116
58 UIKit 0x000000010649eacb -[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:] + 1503
59 UIKit 0x00000001064a6d5e __43-[UIInputWindowController setInputViewSet:]_block_invoke.1318 + 97
60 UIKit 0x000000010649a59a -[UIInputWindowController performOperations:withTemplateNotificationInfo:] + 46
61 UIKit 0x00000001064a68ea -[UIInputWindowController setInputViewSet:] + 1753
62 UIKit 0x000000010649e14c -[UIInputWindowController performOperations:withAnimationStyle:] + 50
63 UIKit 0x0000000106114a8a -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1505
64 UIKit 0x0000000106115c4b -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:reset:] + 499
65 UIKit 0x0000000105bec35d -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 1145
66 UIKit 0x0000000105bedfae -[UIViewController _presentViewController:withAnimationController:completion:] + 4660
67 CoreFoundation 0x00000001044b2c6c __invoking___ + 140
68 CoreFoundation 0x00000001044b2b40 -[NSInvocation invoke] + 320
69 UIKit 0x0000000105bd1633 -[_UIDelayedPresentationContext finishDelayedPresentation:] + 230
70 UIKit 0x0000000105be9416 -[UIViewController _endDelayingPresentation] + 93
71 CoreFoundation 0x00000001044b2c6c __invoking___ + 140
72 CoreFoundation 0x00000001044b2b40 -[NSInvocation invoke] + 320
73 FrontBoardServices 0x000000010a1f25f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
74 FrontBoardServices 0x000000010a1f246d -[FBSSerialQueue _performNext] + 186
75 FrontBoardServices 0x000000010a1cb360 -[FBSWorkspace synchronizeSystemAnimationFencesWithCleanUpBlock:] + 1549
76 UIKit 0x0000000105a311d4 -[UIApplication _synchronizeSystemAnimationFencesWithSpinCleanUpBlock:] + 543
77 UIKit 0x0000000105ab22cd __realPreCommitHandler_block_invoke + 395
78 QuartzCore 0x000000010575532c _ZNK2CA11Transaction5Fence13run_callbacksEv + 40
79 QuartzCore 0x0000000105727f7c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 528
80 QuartzCore 0x0000000105754130 _ZN2CA11Transaction6commitEv + 468
81 QuartzCore 0x0000000105754b37 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 115
82 CoreFoundation 0x00000001044d0717 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
83 CoreFoundation 0x00000001044d0687 __CFRunLoopDoObservers + 391
84 CoreFoundation 0x00000001044b5720 __CFRunLoopRun + 1200
85 CoreFoundation 0x00000001044b5016 CFRunLoopRunSpecific + 406
86 GraphicsServices 0x000000010a9eda24 GSEventRunModal + 62
87 UIKit 0x0000000105a38134 UIApplicationMain + 159
88 trustedfamily-ios 0x0000000101ff6837 main + 55
89 libdyld.dylib 0x000000010894465d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Try to make your html convert in NSAttributedString inside
DispatchQueue.main.async {
}
I'm seeing this crash in a very similar situation in the field but can't reproduce it myself.
Let's try this and see if it helps:
guard UIApplication.shared.applicationState == .active else { return NSAttributedString() }

What's the cause of crash "Crashed: com.apple.main-thread EXC_BREAKPOINT"

I'm getting this crash on crashlytics (fabric), but unable to reproduce it or understand its origin based on the traceback.
Crashed: com.apple.main-thread
0 MyApp 0x1001080a0 MyViewController.viewWillAppear(Bool) -> () (MyViewController.swift:135)
1 MyApp 0x1001080d0 #objc MyViewController.viewWillAppear(Bool) -> () (MyViewController.swift)
2 UIKit 0x18868bbf4 -[UIViewController _setViewAppearState:isAnimating:] + 632
3 UIKit 0x18868b964 -[UIViewController __viewWillAppear:] + 156
4 UIKit 0x1888101d4 -[UINavigationController _startCustomTransition:] + 1144
5 UIKit 0x18872ab78 -[UINavigationController _startDeferredTransitionIfNeeded:] + 676
6 UIKit 0x18872a7e0 -[UINavigationController __viewWillLayoutSubviews] + 64
7 UIKit 0x18872a744 -[UILayoutContainerView layoutSubviews] + 188
8 UIKit 0x18867107c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1200
9 QuartzCore 0x185861274 -[CALayer layoutSublayers] + 148
10 QuartzCore 0x185855de8 CA::Layer::layout_if_needed(CA::Transaction*) + 292
11 QuartzCore 0x185855ca8 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32
12 QuartzCore 0x1857d134c CA::Context::commit_transaction(CA::Transaction*) + 252
13 QuartzCore 0x1857f83ac CA::Transaction::commit() + 504
14 UIKit 0x1888f2524 _UIApplicationFlushRunLoopCATransactionIfTooLate + 172
15 UIKit 0x188e689f8 __handleEventQueue + 4916
16 UIKit 0x188e68b9c __handleHIDEventFetcherDrain + 148
17 CoreFoundation 0x1824f142c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
18 CoreFoundation 0x1824f0d9c __CFRunLoopDoSources0 + 540
19 CoreFoundation 0x1824ee9a8 __CFRunLoopRun + 744
20 CoreFoundation 0x18241eda4 CFRunLoopRunSpecific + 424
21 GraphicsServices 0x183e88074 GSEventRunModal + 100
22 UIKit 0x1886d9058 UIApplicationMain + 208
23 MyApp 0x1009052fc main (AppDelegate.swift:16)
24 libdyld.dylib 0x18142d59c start + 4
The code in MyViewController:
#IBOutlet weak var someConstraint: NSLayoutConstraint!
#IBOutlet weak var button: UIButton!
let minHeight: CGFloat = 2.0
let cValue: CGFloat = 32.0
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let aValue = button.frame.origin.y - UIApplication.shared.statusBarFrame.height
let bValue = button.bounds.height + minHeight
someConstraint.constant = max(aValue - bValue, cValue) // --> Crash here, line 135
}
Update: SomeConstraint is not nil.

Scroll to bottom when keyboard appears causing crash

I want to scroll my tableview to bottom when keyboard appears. But i am getting lot of crash reports from my users.
My code:
func keyboardWillShow(_ notification: Foundation.Notification) {
var info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
self.view.layoutIfNeeded()
UIView.animate(withDuration: 0.2, animations: {
self.bottomCons.constant = keyboardFrame.size.height
self.view.layoutIfNeeded()
})
let numberOfRows = tableView.numberOfRows(inSection: 0)
if numberOfRows != 0 {
do {
try tableView.scrollToRow(at: IndexPath(row: numberOfRows-1, section: 0), at: .bottom, animated: false)
}catch {
print("error")
}
}
}
Crash log:
Fatal Exception: NSRangeException
0 CoreFoundation 0x18f3bafe0 __exceptionPreprocess
1 libobjc.A.dylib 0x18de1c538 objc_exception_throw
2 CoreFoundation 0x18f299200 -[__NSArrayM removeObjectAtIndex:]
3 UIKit 0x195826f90 -[UITableView _existingCellForRowAtIndexPath:]
4 UIKit 0x195836a50 -[UITableView _heightForRowAtIndexPath:]
5 UIKit 0x1956291c0 -[UISectionRowData heightForRow:inSection:canGuess:]
6 UIKit 0x195628ebc -[UITableViewRowData rectForRow:inSection:heightCanBeGuessed:]
7 UIKit 0x19562c21c -[UITableView rectForRowAtIndexPath:]
8 UIKit 0x195704180 -[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]
9 UIKit 0x195703c84 -[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]
10 Ribony 0x1000a3044 ChatViewController.keyboardWillShow(Notification) -> () (ChatViewController.swift)
11 Ribony 0x1000a07d4 #objc ChatViewController.showConnectedLayout(Notification) -> ()
12 CoreFoundation 0x18f3555f4 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__
13 CoreFoundation 0x18f354d08 _CFXRegistrationPost
14 CoreFoundation 0x18f354a84 ___CFXNotificationPost_block_invoke
15 CoreFoundation 0x18f3c37a8 -[_CFXNotificationRegistrar find:object:observer:enumerator:]
16 CoreFoundation 0x18f29895c _CFXNotificationPost
17 Foundation 0x18fdaa930 -[NSNotificationCenter postNotificationName:object:userInfo:]
18 UIKit 0x195ed0190 -[UIInputWindowController postStartNotifications:withInfo:]
19 UIKit 0x195ed0edc __48-[UIInputWindowController viewDidLayoutSubviews]_block_invoke
20 UIKit 0x195ecdc04 -[UIInputWindowController performWithSafeTransitionFrames:]
21 UIKit 0x195ed098c -[UIInputWindowController viewDidLayoutSubviews]
22 UIKit 0x1954e90f4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
23 QuartzCore 0x1926d9274 -[CALayer layoutSublayers]
24 QuartzCore 0x1926cdde8 CA::Layer::layout_if_needed(CA::Transaction*)
25 UIKit 0x1954fd814 -[UIView(Hierarchy) layoutBelowIfNeeded]
26 UIKit 0x195ecc4cc -[UIInputSetHostView layoutIfNeeded]
27 UIKit 0x195c79310 -[_UIRemoteKeyboards controllerDidLayoutSubviews:]
28 UIKit 0x195d49364 -[UICompatibilityInputViewController viewDidLayoutSubviews]
29 UIKit 0x1954e90f4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
30 QuartzCore 0x1926d9274 -[CALayer layoutSublayers]
31 QuartzCore 0x1926cdde8 CA::Layer::layout_if_needed(CA::Transaction*)
32 UIKit 0x1954fd814 -[UIView(Hierarchy) layoutBelowIfNeeded]
33 UIKit 0x195ecc4cc -[UIInputSetHostView layoutIfNeeded]
34 UIKit 0x195ed4da0 -[UIInputWindowController _updateBackdropViews]
35 UIKit 0x195d4823c -[UICompatibilityInputViewController setInputMode:]
36 CoreFoundation 0x18f3555f4 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__
37 CoreFoundation 0x18f354d08 _CFXRegistrationPost
38 CoreFoundation 0x18f354a84 ___CFXNotificationPost_block_invoke
39 CoreFoundation 0x18f3c37a8 -[_CFXNotificationRegistrar find:object:observer:enumerator:]
40 CoreFoundation 0x18f29895c _CFXNotificationPost
41 Foundation 0x18fdaa930 -[NSNotificationCenter postNotificationName:object:userInfo:]
42 UIKit 0x195908be4 -[UIKeyboardImpl setKeyboardInputMode:userInitiated:updateIndicator:executionContext:]
43 UIKit 0x19590a388 -[UIKeyboardImpl setInputModeToNextInPreferredListWithExecutionContext:]
44 UIKit 0x1956aed88 -[UIKeyboardLayoutStar completeSendStringActionForTouchUp:withActions:timestamp:interval:didLongPress:prevActions:executionContext:]
45 UIKit 0x1956a1f18 -[UIKeyboardLayoutStar completeRetestForTouchUp:timestamp:interval:executionContext:]
46 UIKit 0x19592bbb8 __45-[UIKeyboardLayout touchUpTaskForTouchState:]_block_invoke
47 UIKit 0x195515e0c -[UIKeyboardTaskQueue continueExecutionOnMainThread]
48 UIKit 0x19592b868 -[UIKeyboardLayout _touchEndedProcessingForTouches:]
49 UIKit 0x195520390 -[UIWindow _sendTouchesForEvent:]
50 UIKit 0x19551b728 -[UIWindow sendEvent:]
51 UIKit 0x1954ec33c -[UIApplication sendEvent:]
52 UIKit 0x195ce6014 __dispatchPreprocessedEventFromEventQueue
53 UIKit 0x195ce0770 __handleEventQueue
54 UIKit 0x195ce0b9c __handleHIDEventFetcherDrain
55 CoreFoundation 0x18f36942c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
56 CoreFoundation 0x18f368d9c __CFRunLoopDoSources0
57 CoreFoundation 0x18f3669a8 __CFRunLoopRun
58 CoreFoundation 0x18f296da4 CFRunLoopRunSpecific
59 GraphicsServices 0x190d00074 GSEventRunModal
60 UIKit 0x195551058 UIApplicationMain
61 Ribony 0x10003f11c main (AppDelegate.swift:15)
62 libdyld.dylib 0x18e2a559c start
How can i resolve this issue?
I am convinced problem lays here
UIView.animate(withDuration: 0.2, animations: {
self.bottomCons.constant = keyboardFrame.size.height
self.view.layoutIfNeeded()
})
...
try tableView.scrollToRow(at: IndexPath(row: numberOfRows-1, section: 0), at: .bottom, animated: false)
You are reloading layout and trying to scroll in the very same moment, you should scroll afterwards or before. Might be I am not right, but this is usually the problem.

Swift crash reports don't make sense to me

This is the crash log I got from Crashlytics:
Thread : Crashed: com.apple.main-thread
0 Trenìt! 0x1000b93e4 SearchHistoryProvider.getMostRecentStations(Int) -> [String] (SearchHistoryProvider.swift)
1 Trenìt! 0x10007985c specialized MasterViewController.onTouchedTextField(UITextField) -> () (MasterViewController.swift:265)
2 Trenìt! 0x100075414 #objc MasterViewController.onTouchedTextField(UITextField) -> () (MasterViewController.swift)
3 UIKit 0x186fa0ad0 -[UIApplication sendAction:to:from:forEvent:] + 100
4 UIKit 0x186fa0a4c -[UIControl sendAction:to:forEvent:] + 80
5 UIKit 0x186f88740 -[UIControl _sendActionsForEvents:withEvent:] + 436
6 UIKit 0x186fa9248 -[UIControl touchesBegan:withEvent:] + 400
7 UIKit 0x186f9fdc0 -[UIWindow _sendTouchesForEvent:] + 376
8 UIKit 0x186f98b08 -[UIWindow sendEvent:] + 784
9 UIKit 0x186f68f4c -[UIApplication sendEvent:] + 248
10 UIKit 0x186f67528 _UIApplicationHandleEventQueue + 6568
11 CoreFoundation 0x181dd5124 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
12 CoreFoundation 0x181dd4bb8 __CFRunLoopDoSources0 + 540
13 CoreFoundation 0x181dd28b8 __CFRunLoopRun + 724
14 CoreFoundation 0x181cfcd10 CFRunLoopRunSpecific + 384
15 GraphicsServices 0x1835e4088 GSEventRunModal + 180
16 UIKit 0x186fd1f70 UIApplicationMain + 204
17 Trenìt! 0x10009b2fc main (AppDelegate.swift:14)```
(edited)
and this is my code:
MasterViewController.swift:
263 func showRecentStations(textField: UITextField) {
264 textField.text = ""
265 suggestionStations = masterContainerManager!.homeController?.searchHistoryProvider?.getMostRecentStations(10)
266 updateStationsTable(textField)
267 }
SearchHistoryProvider.swift
func getMostRecentStations (maxSize : Int) -> [String] {
let stationsByOldestArray = getRecentStationsByOldest()
let stations = NSMutableOrderedSet()
for i in (0...(stationsByOldestArray.count-1)).reverse() {
stations.addObject(stationsByOldestArray[i].depStation)
if stations.count==maxSize {
return stations.array as! [String]
}
stations.addObject(stationsByOldestArray[i].arrStation)
if stations.count==maxSize {
return stations.array as! [String]
}
}
return stations.array as! [String]
}
Can anyone understand what's the crash about?
I found the bug:
in case stationsByOldestArray.count was 0
the for loop would have been
for i in (0...-1).reverse()
I fixed it by rewriting the loop as
for i in (0..<stationsByOldestArray.count).reverse()
However I think the Swift crash reports are often hopelessly unhelpful!

Resources