3

I have a Router Helper function that is used to make all the REST calls within the application. I'm using Alamofire for my REST calls. A recently published app has gotten a couple of crashes that I'm not able to make sense of. During the beta testing, this crash never displayed. In the organizer, the crash log shows for the thread:

0    MyApp       Router.URLRequest.getter
1    MyApp       protocol witness for URLRequestConvertible.URLRequest.getter in conformance Router
2    Alamofire   request(URLRequestConvertible) -> Request
...

The helper class generates a URLRequestConvertible object which is used for the REST call.

enum Router: URLRequestConvertible {

    case Get(query: String, params: [String: AnyObject]?)
    case Post(query: String, params: [String: AnyObject]?)
    case Put(query: String, params: [String: AnyObject]?, encoding: ParameterEncoding)
    case Delete(query: String, params: [String: AnyObject]?)

    var URLRequest: NSMutableURLRequest {
        var encodeMethod: Alamofire.ParameterEncoding = Alamofire.ParameterEncoding.JSON

        // Default to GET
        var httpMethod: String = Alamofire.Method.GET.rawValue

        let (path, parameters): (String, [String: AnyObject]?) = {
            switch self {
            case .Get(let query, let params):
                // Set the request call
                httpMethod = Alamofire.Method.GET.rawValue
                // Return the query
                return (query, params)
            case .Post(let query, let params):
                // Set the request call
                httpMethod = Alamofire.Method.POST.rawValue
                // Return the query
                return (query, params)
            case .Put(let query, let params, let encoding):
                // Set the request call
                httpMethod = Alamofire.Method.PUT.rawValue
                // Set the encoding
                encodeMethod = encoding
                // Return the query
                return (query, params)
            case .Delete(let query, let params):
                // Set the request call
                httpMethod = Alamofire.Method.DELETE.rawValue
                // Return the query
                return (query, params)
            }
            }()


        // Create the URL Request
        let URLRequest = NSMutableURLRequest(URL: NSURL(string: Globals.BASE_URL + path)!)
        // set header fields
        if let key = NSUserDefaults.standardUserDefaults().stringForKey(Globals.NS_KEY_SESSION) {
            URLRequest.setValue(key, forHTTPHeaderField: "X-XX-XXX")
        }
        // Add user agent
        if let userAgent = NSUserDefaults.standardUserDefaults().stringForKey(Globals.NS_KEY_USER_AGENT) {
            URLRequest.setValue(userAgent, forHTTPHeaderField: "User-Agent")
        }

        // Set the HTTP method
        URLRequest.HTTPMethod = httpMethod

        return encodeMethod.encode(URLRequest, parameters: parameters).0
    }
}

When opening up the stack trace, the line which the crash points to is

let URLRequest = NSMutableURLRequest(URL: NSURL(string: Globals.BASE_URL + path)!)

where the Globals.BASE_URL is a static constant value.

Does anyone know what could be causing this crash?

Mike Walker
  • 2,944
  • 8
  • 30
  • 62
  • is the crash a EXC_BREAKPOINT? if so it may refer to "NSURL(string: Globals.BASE_URL + path)" being nil and you are forcing unwrapping on it. try to protect against this case – Tomer Even Aug 29 '16 at 06:52

0 Answers0