Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First-element-marker empty-list String. Crash happens only in production

Tags:

ios

swift

So I have been pulling my hair out over this mysterious issue (at least for me). It only happens on App Store. I tried my best to check out my BookingViewModel codes, and I am convinced that there should be no crash there, so I did some cleaning a bit, then uploaded a new build to TestFlight, and crash happened again.

Here's a screenshot of the crash logs from Fabric's Crashlytics:

enter image description here

and a piece of the stacktrace:

#0. Crashed: com.apple.main-thread
0  libswiftCore.dylib             0x103e70314 swift_unknownRelease + 24
1  libswiftCore.dylib             0x103e37b5c swift_arrayDestroy + 68
2  libswiftCore.dylib             0x103c308c0 (Missing)
3  libswiftCore.dylib             0x103e40f04 swift_unownedCheck + 88
4  appname                        0x102940848 BookingViewModelBookingViewModelBooking first-element-marker  empty-list String (BookingViewModel.swift:167)

Lastly, the code from my BookingViewModel that has the line 167 described in the crashlog (I have edited the code for this question, I removed some lines but the idea is still there, also to make the code short):

init(_ booking: Booking, withCompleteInfoCompletion completeInfo: @escaping BookingViewModelCompleteInfoCompletion) {
        let createdDate = booking.createdAt ?? ""
        let username = booking.username ?? ""
        let firstName = booking.firstName ?? ""
        let lastName = booking.lastName ?? ""
        let age = booking.age ?? ""
        let birthdate = booking.birthdate ?? ""

        // Final values for completion

        let userDetails = "\(firstName) \(lastName) \(age) / \(birthdate)".condensedWhitespace

        // Booking Status and total amount

        var bookingStatus: BookingStatus = .forProcessing

        if let status = booking.status,
            let statusId = status.id,
            let newBookingStatus = BookingStatus(rawValue: statusId) {
            bookingStatus = newBookingStatus
        }

        var totalAmount: Double = 0.00

        if bookingStatus == .forPayment || bookingStatus == .paid || bookingStatus == .forConfirmation {
            if let amounts = booking.amount {
                for amount in amounts {
                    if let amountString = amount.amount,
                        let amountDouble = Double(amountString) {
                        totalAmount = totalAmount + amountDouble
                    }
                }
            }
        }

        self.subtotal = String(format: "%.2f", arguments: [totalAmount])

        // Payment Method

        var paymentType: PaymentType = .cod
        if let paymentMethod = booking.paymentMethod,
            let type = PaymentType(rawValue: paymentMethod) {
            paymentType = type
        }

        // Initial Total

        let initialTotal = "₱\(self.getTotalAmount(.paypal))"

        completeInfo(
            "₱\(self.subtotal)", // line 167
            userDetails
        )
    }

So question is: Is there an existing idea as to how could an app crash when it's downloaded from the App Store or Testlfight, but not when in development/debug mode (running in Xcode)?

Also, is it possible to simulate the state of the project when it's on the App Store (e.g. I've read someone's answer on SO that I could change the code signing of the project to iOS Distribution)?

Thank you!

like image 303
Glenn Posadas Avatar asked Oct 28 '22 08:10

Glenn Posadas


1 Answers

Okay, if there's someone out there being bugged by such kind of issue, putting the line that causes the weird crash in the main thread helps!

I am not sure why though, maybe I should inspect the code more. So again, I just had to put the crashing line into the main thread like so:

DispatchQueue.main.async {
    self.bookingViewModel = BookingViewModel(self.booking, withCompleteInfoCompletion: {
    ....
    })
}

And Larme's comment above helped me a lot!

like image 194
Glenn Posadas Avatar answered Nov 15 '22 06:11

Glenn Posadas