7

Having trouble understanding a crash report in Crashlytics.

This is the crash log:

Crashed: com.apple.main-thread
0  Rekindlr                       0x10007a728 ViewController.(user_info(Match?, completionHandler : (Bool?) -> ()) -> ()).(closure #1) (ViewController.swift:201)
1  Alamofire                      0x100156678 partial apply for thunk (ResponseSerialization.swift)
2  Alamofire                      0x10015891c specialized Request.(response<A where ...> (queue : OS_dispatch_queue?, responseSerializer : A, completionHandler : (Response<A.SerializedObject, A.ErrorObject>) -> ()) -> Self).(closure #1).(closure #1) (ResponseSerialization.swift:131)
3  libdispatch.dylib              0x1827614bc _dispatch_call_block_and_release + 24
4  libdispatch.dylib              0x18276147c _dispatch_client_callout + 16
5  libdispatch.dylib              0x182766b84 _dispatch_main_queue_callback_4CF + 1844
6  CoreFoundation                 0x182cccdd8 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
7  CoreFoundation                 0x182ccac40 __CFRunLoopRun + 1628
8  CoreFoundation                 0x182bf4d10 CFRunLoopRunSpecific + 384
9  GraphicsServices               0x1844dc088 GSEventRunModal + 180
10 UIKit                          0x187ec9f70 UIApplicationMain + 204
11 Rekindlr                       0x10007d4e4 main (AppDelegate.swift:17)
12 libdispatch.dylib              0x1827928b8 (Missing)

And the related source:

typealias CompletionHandler = (success: Bool) -> Void


func user_info(match:(Match?), completionHandler:CompletionHandler) {
    var tind_id = match!.tinder_id
    var user_url = "\(USER_INFO_URL)\(tind_id)"
    Alamofire.request(.GET, user_url, headers: ["X-Auth-Token": tinder_token], encoding: .JSON)
        .responseJSON { response in

            let json = JSON(response.result.value!)

            var result = json["results"]
            var distance = result["distance_mi"].intValue

            let realm = try! Realm()

            try! realm.write {
                match?.distance_km = round(Double(distance) / 0.62137)
            }

            completionHandler(success: true)
    }
}

I am guessing I am doing something wrong with how I am using completionHandler? But it doesn't happen to everyone. I can't reproduce the crash, but a few of my users are getting it.

Jonovono
  • 1,979
  • 7
  • 30
  • 53
  • 5
    The immediate suspects are the exclamation marks. At line 10 make sure that value in not null – Uri Brecher May 29 '16 at 03:03
  • 1
    I agree with @UriBrecher. You are saying `try!` twice. That means "please crash me". You can hardly be surprised when Swift does what you asked it to do. – matt May 29 '16 at 03:04

1 Answers1

4

Agreeing with both @Uri and @matt - in case you don't know the do/catch syntax, here is how those lines should be implemented:

do {
  let realm = try Realm()
  try realm.write {
    match?.distance_km = round(Double(distance) / 0.62137)
  }
}catch {
  print(error)
}
sschale
  • 5,168
  • 3
  • 29
  • 36