Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Specialised Crash on UICollectionView cellForItemAtIndexPath

Crashlytics is giving me the following stacktrace. The crash happens inconsistently. Happens on all iOS 9 devices but very infrequently. Unable to figure out the source of the problem. Has not happened on any of the devices I have, been trying to crash for the last 3 days.

Crashed: com.apple.main-thread
0  cherish                        0x10014ee18 specialized PersonalizeViewController.collectionView(UICollectionView, cellForItemAtIndexPath : NSIndexPath) -> UICollectionViewCell (PersonalizeViewController.swift:159)
1  cherish                        0x1001497f0 @objc PersonalizeViewController.collectionView(UICollectionView, cellForItemAtIndexPath : NSIndexPath) -> UICollectionViewCell (PersonalizeViewController.swift)
2  UIKit                          0x188aef3a8 <redacted> + 432
3  UIKit                          0x188311adc <redacted> + 4628
4  UIKit                          0x18830c808 <redacted> + 228
5  UIKit                          0x1882a81e4 <redacted> + 656
6  QuartzCore                     0x185c3a994 <redacted> + 148
7  QuartzCore                     0x185c355d0 <redacted> + 292
8  QuartzCore                     0x185c35490 <redacted> + 32
9  QuartzCore                     0x185c34ac0 <redacted> + 252
10 QuartzCore                     0x185c34820 <redacted> + 500
11 QuartzCore                     0x185c2dde4 <redacted> + 80
12 CoreFoundation                 0x183104728 <redacted> + 32
13 CoreFoundation                 0x1831024cc <redacted> + 372
14 CoreFoundation                 0x1831028fc <redacted> + 928
15 CoreFoundation                 0x18302cc50 CFRunLoopRunSpecific + 384
16 GraphicsServices               0x184914088 GSEventRunModal + 180
17 UIKit                          0x188316088 UIApplicationMain + 204
18 cherish                        0x100142a50 main (AppDelegate.swift:19)
19 libdispatch.dylib              0x182bca8b8 (Missing)

The code where it crashes is:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        if collectionView.tag == 1 {
            (---crash line---) let cell = select_date_collection_view.dequeueReusableCellWithReuseIdentifier(PERSONALIZE_SELECT_DATE_COLLECTION_CELL_IDENTIFIER, forIndexPath: indexPath) as! SelectDateCollectionViewCell 
            // Some changes to cell objects
            return cell
        } else if collectionView.tag == 2 {
            let cell = select_time_collection_view.dequeueReusableCellWithReuseIdentifier(PERSONALIZE_SELECT_TIME_COLLECTION_CELL_IDENTIFIER, forIndexPath: indexPath) as! SelectTimeCollectionViewCell
            // Some changes to cell objects
            return cell
        } else if collectionView.tag == 3 {
            let cell = add_customization_collection_view.dequeueReusableCellWithReuseIdentifier(PERSONALIZE_ADD_CUSTOMIZATION_COLLECTION_CELL_IDENTIFIER, forIndexPath: indexPath) as! AddCustomizationCollectionViewCell
            // Some changes to cell objects
            return cell
        }

        let cell: UICollectionViewCell = UICollectionViewCell()
        return cell
    }

Have initialized all the collection views in the viewDidLoad()

        select_date_collection_view.registerNib(UINib(nibName: "SelectDateCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: PERSONALIZE_SELECT_DATE_COLLECTION_CELL_IDENTIFIER)
        select_date_collection_view.tag = 1
        select_date_collection_view.dataSource = self
        select_date_collection_view.delegate = self
        select_date_collection_view.showsHorizontalScrollIndicator = false
        select_date_collection_view.showsVerticalScrollIndicator = false
        select_date_collection_view.reloadData()

        select_time_collection_view.registerNib(UINib(nibName: "SelectTimeCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: PERSONALIZE_SELECT_TIME_COLLECTION_CELL_IDENTIFIER)
        select_time_collection_view.tag = 2
        select_time_collection_view.dataSource = self
        select_time_collection_view.delegate = self
        select_time_collection_view.showsHorizontalScrollIndicator = false
        select_time_collection_view.showsVerticalScrollIndicator = false
        hideSelectTimeView()

        add_customization_collection_view.registerNib(UINib(nibName: "AddCustomizationCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: PERSONALIZE_ADD_CUSTOMIZATION_COLLECTION_CELL_IDENTIFIER)
        add_customization_collection_view.tag = 3
        add_customization_collection_view.dataSource = self
        add_customization_collection_view.delegate = self
        add_customization_collection_view.showsHorizontalScrollIndicator = false
        add_customization_collection_view.showsVerticalScrollIndicator = false
        hideCustomizationsView()

The error happens inconsistently which is a major cause of concern. As I am unable to figure out what is wrong and the crash logs from itunes or Crashlytics aren't helping.

like image 443
Mudit Jaju Avatar asked May 17 '16 13:05

Mudit Jaju


1 Answers

I finally got my hands on an iphone which was crashing. Turns out there are a couple of problems:

The crash reports of iOS, Crashlytics and iTunes are completely useless. The actual place where the code was crashing was never marked by them but was marked by xcode when it crashed in debug mode. So don't believe on the reports generated by crashlytics or itunes.

The actual problem was that I get a time from the server in the 'HH:mm:ss' format, which I was converting into 'hh:mm a' format since I wanted the AM, PM for my UI. Now the great thing done by iOS is that if the user has his phone's time settings in the 24 Hour format, there would be no AM/PM returned and the app would crash because I was not checking the String returned is nil. To solve the above problem I had to set the locale to:

df.locale = NSLocale(localeIdentifier: "en_US_POSIX")

The problem is finally solved. The main learning is that don't depend on the crashlytics or Itunes reports

like image 105
Mudit Jaju Avatar answered Nov 17 '22 09:11

Mudit Jaju