asked    Lilith     2018-10-22       ios       78 view        1 Answer

[SOLVED] Check for response status without Alamofire

I followed a tutorial to how fetch something from Firebase. The guy in the video said you maybe should check for the response status of 200 (HTTP OK). What are the benefits from checking this and is there a way without Alamofire?

Code looks like this:

guard let url = URL(string: profileImageUrl) else { return }

URLSession.shared.dataTask(with: url) { (data, response, err) in
    if let err = err { print("Failed to fetch the profile image:", err); return }

    //check for response status here

    guard let data = data else { return }
    let image = UIImage(data: data)

    DispatchQueue.main.async {
        self.profileImage.image = image
    }

    }.resume()
}

  1 Answer  

        answered    Susie     2018-10-22      

Most APIs we use have their own sets of status code returned when requesting the content. For simple use like your example it should be sufficient to not make use of it, but in large applications the code has their meanings and the client should handle them properly. There are many resources from which you can read about the advantages of status codes.

You can check the status code from URLSession Task like so:

guard let url = URL(string: profileImageUrl) else { return }

URLSession.shared.dataTask(with: url) { (data, response, err) in
  if let err = err { print("Failed to fetch the profile image:", err); return }

   //check for response status here
   if let httpResponse = response as? HTTPURLResponse {
        print(httpResponse.statusCode)
    }

  guard let data = data else { return }
  let image = UIImage(data: data)

  DispatchQueue.main.async {
      self.profileImage.image = image
  }

  }.resume()
}

Since HTTPURLResponse is subclass of URLResponse and is responsible for accessing info about HTTP load you could easily cast the response and get the statusCode





Your Answer





 2018-10-22         Brandon

Patching Items Doesn't Update Validity in Angular

I've a problem because after i submit the form, even though there's a value, the "field is required" doesn't disappear. It supposed to disappear. Is there something wrong with my validity? Please see this link See this link TSpatchValues(id, i) {let x = (<FormArray>this.addForm.controls['rows']).at(i);const selectedIngredient = this.ingredients.find(y => y.id == id);x.patchValue({ unit_price: selectedIngredient.price});} In these cases, you have to trigger a validity check with (for example) : x.patchValue({ unit_price: selectedIngredient.price});x.get('unit_...
 angular                     2 answers                     56 view
 2018-10-22         Norman

Month year picker in angular4 validation error

I am New in Angular4. I want a Month and year picker. I try this<dp-date-picker #searchFromPicker mode="month" theme="dp-material" onkeydown="return false" (onChange)="onChangeSearchFrom($event)"></dp-date-picker><button class="glyphicon glyphicon-calendar" style="padding-top:5px; padding-bottom:5px;" (click)="openSearchFrom()" [disabled]="!editable"></button>.But validation such as disable,min,max etc is not working in this example.Refer any better option if available other than this. Please help me. Try mater...
 angular                     1 answers                     56 view
 2018-10-22         Allen

How can i integrate chat feature in my Angular4 application?

I want to integrate chatting feature in Angular4 application with text message, smilies, attachment sharing, group chat etc.Chatting between the users of my application. Users list is available from my database. Want to do chat among them.Is there any open source frameworks for this?Please assist You can achieve that by Integrating Web Sockets, Node & Angular WebSockets is a protocol that provides a bi-directional communication channel. This means that a browser and web server can maintain real-time comms, sending messages back and forth while the connection is open...
 angular                     1 answers                     56 view