[iOS] Preparing your UI to run in the background

Hyunndyยท2023๋…„ 2์›” 19์ผ
0

iOS-App-Structure

๋ชฉ๋ก ๋ณด๊ธฐ
4/8

๐Ÿธ

์ €๋ฒˆ ๊ธ€์—์„œ๋Š” App์ด Inactive, Active ์ƒํƒœ๊ฐ€ ๋˜๋ฉฐ ๋ถˆ๋ฆฌ๋Š” delegate ํ•จ์ˆ˜์— ๋Œ€ํ•ด ๋ณด์•˜๋Š”๋ฐ์š”,
์ด๋ฒˆ์—” Preparing your UI to run in the background๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


๊ฐœ์š”

์•ฑ์€ ์ˆ˜๋งŽ์€ ์ด์œ ๋กœ Background ์ƒํƒœ๋กœ ์ „ํ™˜๋ฉ๋‹ˆ๋‹ค.
์‚ฌ์šฉ์ž๊ฐ€ foreground ์•ฑ์„ ์ข…๋ฃŒ์‹œํ‚ค๋ฉด ์•ฑ์€ UIKit์ด suspend ๋˜๊ธฐ์ „์— ์ž ์‹œ Background ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
์‹œ์Šคํ…œ์€ ์•ฑ์„ Background ์ƒํƒœ๋กœ directํ•˜๊ฒŒ launch ํ•˜๊ฑฐ๋‚˜,
Suspendend๋œ ์•ฑ์„ Background ์ƒํƒœ๋กœ ์ด๋™์‹œํ‚ต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ค‘์š”ํ•œ Task๋ฅผ ์ˆ˜ํ–‰ํ•  ์‹œ๊ฐ„์„ ์ค๋‹ˆ๋‹ค.

์•ฑ์ด Background ์ƒํƒœ๊ฐ€ ๋  ๋•Œ, ๊ฐ€๋Šฅํ•˜๋ฉด ์•„๋ฌด๊ฒƒ๋„ ์•ˆํ•˜๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค.
์ง์ „ ์ƒํƒœ๊ฐ€ foreground ์˜€๋‹ค๋ฉด, ๊ณต์œ  ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์ œํ•˜๊ณ  ๋ชจ๋“  Task๋ฅผ ๋ฉˆ์ถ”๊ธฐ ์œ„ํ•ด Background transition์„ ์‚ฌ์šฉํ•˜์„ธ์š”.

๋งŒ์ผ ์ค‘์š”ํ•œ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด Background ์ƒํƒœ๋กœ ์ง„์ž…ํ•œ๋‹ค๋ฉด ๊ฐ€๋Šฅํ•œ ๋น ๋ฅด๊ฒŒ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋น ์ ธ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.
iOS 13 ์ด์ƒ์ด๋ฉด SceneDelegate ๊ฐ์ฒด,
iOS 12 ์ดํ•˜๋ฉด AppDelegate ๊ฐ์ฒด๋กœ ์ด๋ฒคํŠธ๋ฅผ ํ†ต์ง€ํ•ฉ๋‹ˆ๋‹ค.


Quiet your app upon deactivation

์‹œ์Šคํ…œ์€ ๊ฐ–๊ฐ€์ง€ ์ด์œ ๋กœ ์•ฑ์„ ๋น„ํ™œ์„ฑํ™”์‹œํ‚ต๋‹ˆ๋‹ค.
์œ ์ €๊ฐ€ foreground ์•ฑ์„ ๋‚˜๊ฐˆ ๋•Œ, ์‹œ์Šคํ…œ์€ Background ์ƒํƒœ๋กœ ์ด๋™ํ•˜๊ธฐ์ „์— ์•ฑ์„ ์ฆ‰์‹œ ๋น„ํ™œ์„ฑํ™” ์‹œํ‚ต๋‹ˆ๋‹ค.
์‹œ์Šคํ…œ์€ ๋˜ํ•œ ์ผ์‹œ์ ์œผ๋กœ ์ธํ„ฐ๋ŸฝํŠธ๊ฐ€ ํ•„์š”ํ•  ๋•Œ ์•ฑ์„ ๋น„ํ™œ์„ฑํ™” ํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค๋ฉด..

  • ์‹œ์Šคํ…œ Alert (๋ฐฐํ„ฐ๋ฆฌ, ์žฌ๋‚œ ๋ฌธ์ž ๋“ฑ)
  • ์ „ํ™”, ๋ฌธ์ž, ์•Œ๋žŒ ๋“ฑ์ด ์šธ๋ฆด ๋•Œ

์‹œ์Šคํ…œ ํŒจ๋„์˜ ๊ฒฝ์šฐ, ์‹œ์Šคํ…œ์€ ์œ ์ €๊ฐ€ ํŒจ๋„์„ ํ•ด์ œํ•  ๋•Œ ์•ฑ์„ ๋‹ค์‹œ ํ™œ์„ฑํ™” ์‹œํ‚ต๋‹ˆ๋‹ค.
(๐Ÿ‘ฉโ€๐Ÿ’ป ์‹œ์Šคํ…œ ํŒจ๋„์ด ๋ชจ์ง€..)

๋น„ํ™œ์„ฑํ™” ๋™์•ˆ, UIkit์€ ๋‹ค์Œ delegate ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

func sceneWillResignActive(_ scene: UIScene) { ... }
func applicationWillResignActive(_ application: UIApplication) { ... }

์ฃผ์š” ์ž‘์—…์„ ์ผ์‹œ์ •์ง€ ์‹œํ‚ค๋ฉด์„œ ์‚ฌ์šฉ์ž์˜ data๋ฅผ ๋ณด์กดํ•˜๊ณ  ์•ฑ์„ quiteํ•œ ์ƒํƒœ๋กœ ๋‘๋ ค๋ฉด ๋น„ํ™œ์„ฑํ™” ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
ํŠนํžˆ!

  • ์—ด๋ ค์žˆ๋Š” ํŒŒ์ผ ๋‹ซ๊ณ  user data๋ฅผ disk์— ์ €์žฅํ•œ๋‹ค.
  • dispatch ๋ฐ operation queue๋“ค์„ ์ •์ง€ํ•œ๋‹ค.
  • ์ƒˆ๋กœ์šด Task๋ฅผ ์‹คํ–‰์‹œํ‚ฌ ์Šค์ผ€์ค„๋ง์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ํƒ€์ด๋จธ๋ฅผ ๊บผ๋ผ!
  • ๊ฒŒ์ž„ํ”Œ๋ ˆ์ด ์ž๋™์œผ๋กœ ๊บผ๋ผ!
  • ์ƒˆ Metal ์ž‘์—…์„ ์‹œ์ž‘ํ•˜์ง€๋งˆ๋ผ? -> invalidate graphics rendering callbaks.
  • OpenGL ์ปค๋งจ๋“œ๋ฅผ ๋‚ ๋ฆฌ์ง€๋งˆ๋ผ? -> invalidate graphics rendering callbaks.

Release resources upon entering the background

์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์ „ํ™˜๋  ๋•Œ, App์ด ์ฅ๊ณ ์žˆ๋˜ ๋ฉ”๋ชจ๋ฆฌ, ๊ณต์œ  ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์ œ์‹œ์ผœ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
ํฌ๊ทธ๋ผ์šด๋“œ -> ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์ „ํ™˜๋˜๊ธฐ ์œ„ํ•ด, ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ๋Š” ๋งค์šฐ! ๋งค์šฐ! ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

ํฌ๊ทธ๋ผ์šด๋“œ๋Š” ๋ฉ”๋ชจ๋ฆฌ์™€ ์‹œ์Šคํ…œ ์ž์›์— ์šฐ์„ ๊ถŒ์„ ๊ฐ–์ง€๋งŒ, ์‹œ์Šคํ…œ์€ ๋ฆฌ์†Œ์Šค๋ฅผ ํ• ๋‹นํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์•ฑ์„ ํ•„์š”ํ•˜๋ฉด ์ œ๊ฑฐํ•ด๋ฒ„๋ฆฝ๋‹ˆ๋‹ค.
์•ฑ์ด ์ „์— ํฌ๊ทธ๋ผ์šด๋“œ์— ์žˆ์ง€ ์•Š๋”๋ผ๋„ ๊ฐ€๋Šฅํ•œ ์ ์€ ๋ฆฌ์†Œ์Šค๋งŒ์„ ์†Œ๋น„ํ•˜๊ณ  ์žˆ์—ˆ๋Š”์ง€ ํ™•์‹คํžˆ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค~!

๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ๋กœ ์ง„์ž…ํ•  ๋•Œ, UIKit์€ ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

func sceneDidEnterBackground(_ scene: UIScene) { ... }
func applicationDidEnterBackground(_ application: UIApplication) { ... }    

๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ „ํ™˜์ด ์ผ์–ด๋‚  ๋™์•ˆ, ์•„๋ž˜ ์ž‘์—…๋“ค์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • file๋กœ ๋ถ€ํ„ฐ ์ง์ ‘ ์ฝ๋Š” ์ด๋ฏธ์ง€, ๋ฏธ๋””์–ด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  • ๋””์Šคํฌ๋กœ๋ถ€ํ„ฐ reload ํ•  ์ˆ˜ ์žˆ๊ฑฐ๋‚˜ recreate ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ํฌ๊ณ , ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ์™€์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  • ์นด๋ฉ”๋ผ๋‚˜ ๋‹ค๋ฅธ ํ•˜๋“œ์›จ์–ด ๋ฆฌ์†Œ์Šค ์ ‘๊ทผ์„ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค.
  • ์•ฑ์˜ UI์—์„œ password ๊ฐ™์€ ๋ฏผ๊ฐํ•œ ์ •๋ณด๋ฅผ ๊ฐ์ถฅ๋‹ˆ๋‹ค.
  • ์•Œ๋Ÿฟ์ด๋‚˜ ์ผ์‹œ์ ์ธ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค.
  • ๊ณต์œ  ์‹œ์Šคํ…œ DB์™€์˜ ์—ฐ๊ฒฐ์„ ๋Š์Šต๋‹ˆ๋‹ค.
  • Bonjour ์„œ๋น„์Šค(??)๋กœ ๋ถ€ํ„ฐ ๋“ฑ๋ก์„ ํ•ด์ œํ•˜๊ณ  listening ์†Œ์ผ“์„ ๋ชจ๋‘ ๋‹ซ์Šต๋‹ˆ๋‹ค.
  • ๋ชจ๋“  Metal ๋ช…๋ น ๋ฒ„ํผ๊ฐ€ ์Šค์ผ€์ค„๋ง๋˜๋Š” ๊ฒƒ์„ ํ™•์‹คํžˆ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด์ „์— ์ œ์‹œํ–ˆ๋˜ ๋ชจ๋“  OpenGL ๋ช…๋ น์ด ์™„๋ฃŒ๋˜์—ˆ์Œ์„ ํ™•์‹คํžˆ ํ•ฉ๋‹ˆ๋‹ค.

์•ฑ์˜ Asset์—์„œ ๋ฐ›์€ ์ด๋ฏธ์ง€๋ฅผ ์‚ญ์ œํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.
๋น„์Šทํ•˜๊ฒŒ, NSCache, NSDiscardableContent ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ํ•ด์ œํ•  ํ•„์š”๋„ ์—†์Šต๋‹ˆ๋‹ค.
์‹œ์Šคํ…œ์ด ์–˜๋„ค๋“ค์€ ์ž๋™์œผ๋กœ ์ œ๊ฑฐํ•ด์ค๋‹ˆ๋‹ค.

์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ์— ๊ฐˆ ๋•Œ ๊ณต์œ  ์ž์›์„ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š๋„๋ก ํ•˜์‹ญ์‹œ์˜ค!
๋งŒ์ผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์ „ํ™˜๋๋Š”๋ฐ ์นด๋ฉ”๋ผ๋‚˜ DB ๊ฐ™์€ ๊ณต์œ  ์ž์›์— ๊ณ„์† ์ ‘๊ทผํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ์‹œ์Šคํ…œ์€ ๋ฆฌ์†Œ์Šค ํ•ด์ œ๋ฅผ ์œ„ํ•ด ์•ฑ์„ ๊บผ๋ฒ„๋ฆด๊ฒ๋‹ˆ๋‹ค. (ใ… ใ… )


Prepare your UI for the app snapshot

์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ์— ์ง„์ž…ํ•˜๊ณ  delegate ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜๋œ ํ›„์—, UIKit์€ ์•ฑ์˜ ํ˜„์žฌ UI์˜ Snapshot์„ ๋‚จ๊น๋‹ˆ๋‹ค.
์‹œ์Šคํ…œ์€ app switcher์— ์ด ์ด๋ฏธ์ง€๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์•ฑ์„ ํฌ๊ทธ๋ผ์šด๋“œ๋กœ ๋‹ค์‹œ ๋˜๋Œ๋ฆด ๋•Œ ์ผ์‹œ์ ์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

์•ฑ์˜ UI๋Š” ์œ ์ €์˜ ๋ฏผ๊ฐํ•œ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.
์Šค๋ƒ…์ƒท์€ ์œ ์ €๊ฐ€ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋Š” UI๋ฅผ ๋‚˜ํƒ€๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


Respond to important events in the background

์•ฑ์€ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์— ์ง„์ž…ํ•œ ํ›„์—๋Š” ์ถ”๊ฐ€์ ์ธ ์‹คํ–‰ ์‹œ๊ฐ„์„ ํ• ๋‹น ๋ฐ›์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ UIKit์€ ์‹œ๊ฐ„์— ๋ฏผ๊ฐํ•œ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๋Š” ์•ฑ์˜ ๊ฒฝ์šฐ ์‹คํ–‰ ์‹œ๊ฐ„์„ ์Šน์ธํ•ฉ๋‹ˆ๋‹ค.

  • APNs ์ง€์›
  • ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ์˜ ์ •๊ธฐ ์—…๋ฐ์ดํŠธ
  • ๋ธ”๋ฃจํˆฌ์Šค ์•…์„ธ์‚ฌ๋ฆฌ ์™€์˜ ์†Œํ†ต
  • ์™ธ๋ถ€ ๊ธฐ๊ธฐ์™€์˜ ์†Œํ†ต
  • AirPlay, PiP ์ง€์›
  • ์œ„์น˜ ๊ธฐ๋ฐ˜ ์•ฑ
  • IP๋ฅผ ๊ฑฐ์น˜๋Š” ๋ณด์ด์Šค(?)

๋ฐฑ๊ทธ๋ผ์šด๋“œ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” XCode์—์„œ Background Modes Capability๋ฅผ ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๊ฐ Background Task๋Š” ๊ฐ์ž์˜ ์š”๊ตฌ์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค


๋งˆ๋ฌด๋ฆฌ

์•ฑ์ด Background ์ƒํƒœ๋กœ ์ „ํ™˜ ๋  ๋•Œ๋Š”
1. ๋น„ํ™œ์„ฑํ™” ๋Œ€๋น„ ์ง€๊ธˆ ์•ฑ ์ƒํƒœ quiet! ๋ฉˆ์ถฐ!
2. ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๊ฐ€๊ธฐ์ „์— ๊ณต์œ  ๋ฆฌ์†Œ์Šค ๋‹ค ํ•ด์ œํ•ด~~~!! ์•ˆ๊ทธ๋Ÿผ ์ œ๊ฑฐ๋ผ ~~!!

์ด ๋‘ ๊ฐ€์ง€ ๊ด€์ ์œผ๋กœ ๋ถˆ๋ฆฌ๋Š” Delegate ๋ฉ”์„œ๋“œ๊ฐ€ ๋‹ค๋ฅด๋„ค์š”.

profile
https://hyunndyblog.tistory.com/163 ํ‹ฐ์Šคํ† ๋ฆฌ์—์„œ ์ด์‚ฌ ์ค‘

0๊ฐœ์˜ ๋Œ“๊ธ€