[iOS] URLSession

.onNext("Volga")·2023년 1월 16일
0

iOS

목록 보기
4/7

URLSession

URLSession Class 공식문서를 읽고 몇가지 프로퍼티에 대해 알아봅니다.

네트워크 데이터 전송 관련된 그룹을 조정하는 개체입니다.

Declaration

class URLSession: NSObject

URLSession 클래스는 NSObject를 상속한 클래스 개체입니다.

Overview

URLSession클래스나 관련 클래스는 URL로 표시되어있는 Endpoint에서 데이터를 다운로드/업로드 하기 위한 API를 제공합니다.
앱이 실행중이 아니거나, iOS에서 앱이 suspended된 동안 백그라운드에서 다운로드를 수행하기 위해 URLSession관련 API를 사용할 수 있습니다.

추가적으로, URLSessionDelegateURLSessionTaskDelegateauthentication을 지원하고 redirection이나 task completion같은 이벤트를 수신 할 수 있습니다.

  • 앱은 하나 이상의 URLSession 인스턴스를 생성합니다.
  • 각 인스턴스는 data-transfer관련 task그룹을 조정합니다.
    • 예를들어, 웹 브라우저를 만드는 경우 앱에서 탭 또는 window당 하나의 세션을 만듭니다.
  • 각 세션 내에서 앱은 특정 URL에 대한 요청을 나타내는 작업을 추가합니다.

Type of URL Sessions

Types of URL Sessions
The tasks within a given URL session share a common session configuration object, which defines connection behavior, like the maximum number of simultaneous connections to make to a single host, whether connections can use the cellular network, and so on.

URLSession has a singleton shared session (which doesn’t have a configuration object) for basic requests. It’s not as customizable as sessions you create, but it serves as a good starting point if you have very limited requirements. You access this session by calling the shared class method. For other kinds of sessions, you create a URLSession with one of three kinds of configurations:

- A default session behaves much like the shared session, but lets you configure it. You can also assign a delegate to the default session to obtain data incrementally.

- Ephemeral sessions are similar to shared sessions, but don’t write caches, cookies, or credentials to disk.

- Background sessions let you perform uploads and downloads of content in the background while your app isn’t running.

See Creating a Session Configuration Object in the URLSessionConfiguration class for details on creating each type of configuration.

공식 문서 내용은 위와 같습니다.

요약을 간단하게 해봅시다.

  • URLSession에는 basic requests를 위한 공유세션이 있습니다.
  • shared 클래스 메소드를 호출해서 세션에 접근합니다.
  • 다른 유형의 세션의 경우에 다음 세가지 configuration을 사용해서 URLSession을 생성합니다.
    • default sessionshared session과 유사하게 동작합니다.
    • 하지만 개발자가 configure가능합니다.
    • 또한 기본세션에 delegate을 할당해서 데이터를 incrementally로 가져 올 수도 있습니다.
  • Ephemeral sessionshared session과 유사하지만 캐시, 쿠키 또는 자격 증명에 사용되지는 않습니다.
  • Background session을 사용하면 앱이 실행되지 않는 백그라운드 상태에서 Contents Upload & Download 작업을 수행 할 수 있습니다.

Types of URL Session Tasks

Within a session, you create tasks that optionally upload data to a server and then retrieve data from the server either as a file on disk or as one or more NSData objects in memory. The URLSession API provides four types of tasks:

- Data tasks send and receive data using NSData objects. Data tasks are intended for short, often interactive requests to a server.

- Upload tasks are similar to data tasks, but they also send data (often in the form of a file), and support background uploads while the app isn’t running.

- Download tasks retrieve data in the form of a file, and support background downloads and uploads while the app isn’t running.

WebSocket tasks exchange messages over TCP and TLS, using the WebSocket protocol defined in RFC 6455.

  • 데이터 작업은 NSData를 사용해서 데이터를 주고 받습니다.
  • 업로드 tasks는 데이터 작업과 유사하지만, 종종 파일 형식의 데이터도 전송하고 앱이 실행 되지 않는 동안 백그라운드에서 업로드를 지원합니다.
  • 다운로드 작업은 파일 혈식으로 데이터를 검색하고 앱이 실행되지 않는 동안 백그라운드에서 다운로드 & 업로드를 지원합니다.
  • 웹 소켓 Task는 정의된 웹소켓 프로토콜을 사용해서 TCP / TLS를 통해서 메세지를 교환합니다.
profile
iOS 개발자 volga입니다~

0개의 댓글