Skip to content

Latest commit

ย 

History

History
194 lines (119 loc) ยท 4.56 KB

AdvancesInNetworkingPart1.md

File metadata and controls

194 lines (119 loc) ยท 4.56 KB

@ WWDC 19

Low Data Mode

์„ค์ •์—์„œ low data mode๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์˜ต์…˜์„ ์ผฐ์„ ๋•Œ ์•ฑ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•  ์ง€ ๊ฐœ๋ฐœํ•˜๋ฉด์„œ ์„ค์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

  • User preference to minimize data usage
    • Explicit signal to reduce network data use
    • Peer Wi-Fi and Cellular network
  • System plicy
    • Discretionary tasks deferred
    • Background App Refresh disabled

Application Adoption

๋‚ด ์•ฑ์— ์ ์šฉํ•  ๋•Œ!, ์ด ์˜ต์…˜์ด ์ผœ์ ธ ์žˆ๋‹ค๋ฉด

  • Reduce image quality
  • Reduce pre-fetching
  • Synchronize less often
  • Mark tasks discretionary
  • Disble auto-play
  • Do not block user-initiated work

Low Data Mode APIs

  • URLSession
    • Try large/prefetch with allowsConstrainedNetworkAccess = false
    • On failure with error.networkUnavailableReason == .constrained try Low Data Mode alternative
  • Network framework
    • Set prohibitConstrainedPaths on NWParameteers
    • Handle path updatees

Constrained and Expensive

  • Constrained - Low Data Mode
  • Expensive - Cellular and Personal Hotspot
  • URLSession
    • allowsExpensiveNetworkAccess
  • Network.framework
    • Set prohibitExpensivePaths on NWParameters
    • Check isExpensive on NWPath

Combine in URLSession

13๋ถ€ํ„ฐ ๋‚˜์˜จ Combine์„ ์–ด๋–ป๊ฒŒ URLSession๊ณผ ํ•จ๊ป˜ ์“ฐ๋Š”์ง€ ๋ฐ๋ชจ์™€ ํ•จ๊ป˜ ์„ค๋ช…. Combine์— ๋Œ€ํ•ด ์ข€ ์•Œ๊ณ  ๋ด์•ผ ์ดํ•ด๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๋Œ€๊ฐ• Rx ์‚ด์ง ๋ง›๋ดค๋˜ ์ •๋„๋กœ ์ดํ•ด ๊ฐ€๋Šฅ

  • Streamline networking code with Combine
  • Support retry
    • Use low retry count
    • Only idempotent request
  • Best practices for Low Data Mode
// Generalized Publisher for Adaptive URL Loading
func adaptiveLoader(regularURL: URL, lowDataURL: URL) -> AnyPPublisher<Data, Error> {
  var request = URLRequest(url: regularURL)
  request.allowsConstrainedNetworkAccess = false
  return URLSession.shared.dataTaskPublisher(for: request)
  	.tryCatch { error -> URLSession.DataTaskPublisher in
			guard error.networkUnavailableReason == .constrained else {
        throw error
      }
			return URLSession.shared.dataTaskPublisher(for: lowDataURL)
    }
  	
  	.tryMap { data, response -> Data in
			guard let httpResponse = response as? HTTPURLResponse,
				httpResponse.statusCode == 200 else {
          throw MyNetworkingError.invalidServerResponse
        }
				return data
		}
  	.eraseToAnyPublisher()
}

WebSocket

์›น์†Œ์ผ“ ์ด์šฉํ•˜๋ฉด ์–‘๋ฐฉํ–ฅ ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

  • Two-way communication over TLS/TCP connection
  • Works with Firewalls and CDNs
  • Proxy support

์ด๋ฒˆ์— URLSessioon๊ณผ Network.framework์—์„œ ์›น ์†Œ์ผ“์„ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค!

URLSessionWebSocketTask

  • Foundation API for WebSocket
  • Works with existing URLSession
// Create with URL
let task = URLSession.shared.webSocketTask(with: URL(string: "wss://websocket.example")!)
task.resume()

// Send a message
task.send(.string("Hello")) { error in /* Handle error */ }

// Receive a message
task.receive { result in /* Handle result */ }

Network.framework

  • Both client and server support
  • Receive partial or complete WebSocket messages
// Create parameters for WebSocket over TLS
let parameters = NWParameters.tls
let websocketOptions = NWProtocolWebSocket.Options()
parameters.defaultProtoocolStack.applicationProtocols.insert(websocketOptions, at: 0)

// Create a connection with those parameters
let websocketConnection = NWConnection(to: endppoint, using: parameters)

// Create a listener with those parameters
let websocketListener = try NWListener(using: parameters)

WebSocket APIs

  • WebKit
    • JavaScript WebSocket
  • URLSession
    • URLSessionWebSocketTask
  • Network.framework
    • WebSocket Connection
    • WebSocket Listener

Mobilty Improvements

๋„คํŠธ์›Œํฌ๋ฅผ ์ด์•ผ๊ธฐํ•  ๋•Œ, mobility ์ค‘์š”! Wi-Fi Assist, Multipath Transport๊ฐ€ ๊ฐ•ํ•œ iOS 13 ์†Œ๊ฐœ

Wi-Fi Assist in iOS 13

Cross-layer Mobility Detection, Improved Flow Recovery

  • Use high-level APIs like URLSession or Network.framework
  • Rethink SCNetworkReachability usage
  • Control access with allowsExpensiveNetworkAccess = false

Multipath Transports

Responsiveness for Maps, Fewer streaming stalls in Music

  • Multipath Transports for your App
    • multipathServiceType URLSessionConfiguration and Network.framework
  • Server-side configuration

Mobility Improvements

  • Mobility should not impair your Apps
  • Use high-level APIs
  • Rethink interface management
  • Prepare your servers and use multipathServiceType