0% found this document useful (0 votes)
32 views

Swift12019 URL Requests and Data Model Parsing Part 2

The document discusses network response handling in three sentences or less: 1. Network response objects contain data, URLResponse metadata, and an error object that must be checked first to see if an error occurred. 2. If no error, the data must be checked and converted to a usable format like a string, while errors are passed to an onError closure to be handled on the main thread. 3. Successful responses are typically JSON that must be deserialized into structures using Codable to map JSON keys to struct properties.

Uploaded by

Lina Y Harara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Swift12019 URL Requests and Data Model Parsing Part 2

The document discusses network response handling in three sentences or less: 1. Network response objects contain data, URLResponse metadata, and an error object that must be checked first to see if an error occurred. 2. If no error, the data must be checked and converted to a usable format like a string, while errors are passed to an onError closure to be handled on the main thread. 3. Successful responses are typically JSON that must be deserialized into structures using Codable to map JSON keys to struct properties.

Uploaded by

Lina Y Harara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Networking

Handling Responses Mr. Ahmed Qazzaz

last update Dec. 7th., 2019


1
Response Objects

✤ Task completion handler will conations three objects

✤ Data: Which is a binary representation of the server


response

✤ URLResponse: Which is an object contains meta


data about the response such as response headers

✤ Error : Which is an error that contains HTTP error


information if exists.
2
Response Objects

✤ We need to check error object first to find out if there is


an error or not.

✤ If error exists need to handle the error and pass it to


the caller view controller

✤ If no error exists we need to check the data and


handle its content
3
Response Objects
✤ You see, the first line is to
check if there “error” from
response contains a value
What is the content of onError save it in new “error”
Function ??
variable, and pass the
“localized description” to a
function called onError

✤ Localized Description is
the error description in the
app/system language
4
Passing Data Back

✤ Previously we learned about passing data back from


one view controller to a previous one, which is the
protocol (delegate) method.

✤ Closures (callback functions) can also be used in the


same purpose

5
Passing Data Back

✤ The difference between delegates and closures is the


nature of the view controllers relations

✤ If there is single VC will be delegate to another VC, we


can use protocols.

✤ If there will be more than one VC using a function that


returns data after long life process, we will use
closures.
6
onError is a closure !!
ViewController UserServices

7
onError is a closure - 2
ViewController UserServices

8
onError is a closure - 3
ViewController UserServices

9
onError is a closure - 3
ViewController UserServices

10
onError is a closure - 3
ViewController UserServices

11
onError is a closure - 3
ViewController UserServices

12
onError is a closure - 3
ViewController UserServices

13
onError is a closure - 3
ViewController UserServices

14
onError is a closure - 3
ViewController UserServices

15
onError is a closure - 3
ViewController UserServices

16
Waiting for Response

17
Response Received
ViewController UserServices

18
Response Received - it was an error

ViewController UserServices

19
Response Received - it was an error

ViewController UserServices

20
Response Received - it was an error

ViewController UserServices

21
DispatchQueue !! What is this ??

✤ when you send a request the app will continue


executing other codes, and when the response
received a new process will start executing the
response closure synchronously.

✤ The first process that continues the app execution


called the main thread

✤ The new process that handles the response execution


called a background thread
22
DispatchQueue !! What is this ??

✤ Some code commands should always executed in the main


thread such as the UI changes.

✤ So, When we called onError function from the response


handling closure (Background thread)

✤ Inside the function we need to popup an alert telling the


user what is the error was

✤ so showing UIAlert is a UI change so we need to execute


this code in the main thread
23
DispatchQueue

✤ So DispatchQueue.main.async {

✤ //Will execute code segment in the main thread

✤ }

24
OK, the success case.

25
Success Case

✤ First we need to check if the data object contains data.

✤ Then we need to convert the binary data to another


object that we can handle

✤ sometimes we need to convert the data to a string to


see its content

26
Data -> String and vice versa

✤ Using this constructor we can convert data object to a


string

✤ String(data: Data, encoding : Encoding)

✤ to convert it back use the same “str” object

27
Response possibilities

✤ Unusual Responses

✤ HTML code,

✤ Plain text

✤ Normal responses

✤ JSON Object (key and value)

✤ JSON Array
28
Handel normal response

✤ the server always should return a JSON response so


we can handle it

✤ To convert the binary data to array or dictionary we


use the class

✤ JSONSerialization

29
JSONSerialization

✤ This code will try to convert the response data to a


dictionary

✤ After that we can check the keys of the object and act
based on these values

30
JSONSerialization, Reverse

✤ This is the reversed operation we can convert


dictionary or array to binary

31
Convert Dictionary to Struct

✤ In your application JSON responses will be accessed through


objects not dictionaries or strings, se finally we need to create
objects of structs or classes from these responses.

✤ The easiest way is to use structs, but the struct should


implement “Codable” protocol which includes two
processes (Encodable, and Decodable)

✤ Also all variables in the struct should be exactly named as


the JSON keys
32
Codable Struct

33
Dir. to Struct and vise versa

✤ JSONEncoder is the class used to convert struct to dir.

✤ JSONDecoder is the class used to convert dir. to struct

34

You might also like