๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/iOS Programming

[iOS] iOS data storage - 1. Sandbox(์ƒŒ๋“œ๋ฐ•์Šค)

[Contents]

  • SandBox ?
  • SandBox ๊ตฌ์กฐ
    1. Bundle Container
    2. Data Container
      • Document
      • Library
      • tmp
    3. iCloud Container

1. SandBox ?

์•ฑ์„ ์ƒˆ๋กœ ์„ค์น˜ํ–ˆ์„ ๋•Œ, ๊ฐ„ํ˜น ์•„๋ž˜์™€ ๊ฐ™์ด ์ ‘๊ทผ ๊ถŒํ•œ์„ ํ—ˆ์šฉํ•ด๋‹ฌ๋ผ๋Š” Alert์€ ๋ณธ ์ ์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.

 

์ด๋Ÿฌํ•œ ๊ณผ์ •์ด ๊ฐ ์•ฑ๋ณ„๋กœ ๋ณ„๋„๋กœ ์ˆ˜ํ–‰๋˜๋Š”๋ฐ,

๊ฐ ์•ฑ์ด ํŠน์ • ๋ฐ์ดํ„ฐ๋‚˜ ํ•˜๋“œ์›จ์–ด์— ์ ‘๊ทผํ•˜๋ ค๋ฉด *๊ถŒํ•œ (Permission)*์„ ์š”์ฒญํ•ด์•ผ ํ•˜๋ฉฐ, ์‚ฌ์šฉ์ž๊ฐ€ ์Šน์ธํ•ด์•ผ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์ด๋Ÿฌํ•œ ๊ตฌ์กฐ๋ฅผ App Sandbox๋ผ๊ณ  ๋ถˆ๋ฆฌ๋ฉฐ, ๊ฐœ๋ฐœ์ž๋Š” ์• ํ”Œ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ถŒํ•œ๊ณผ ์ ‘๊ทผ ๋ฒ”์œ„ ๋‚ด์—์„œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

 

์ฆ‰, ๊ฐ๊ฐ์˜ ์•ฑ๋งˆ๋‹ค ๊ณ ์œ ํ•œ ์‹คํ–‰ ํ™˜๊ฒฝ์ด ์ œ๊ณต๋˜๋ฉฐ, ์•ฑ์€ ์ด ํ™˜๊ฒฝ ์™ธ๋ถ€์˜ ์‹œ์Šคํ…œ ํŒŒ์ผ์ด๋‚˜ ๋‹ค๋ฅธ ์•ฑ์˜ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.

 

๋”ฐ๋ผ์„œ, ์œ„์™€ ๊ฐ™์ด App Sandbox์˜ ์™ธ๋ถ€์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๋ฐ ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•˜๋ ค๋ฉด Sandbox ์ •์ฑ…์— ๋”ฐ๋ผ ์ ‘๊ทผ ๊ถŒํ•œ์„ ๋ถ€์—ฌ๋ฐ›์•„์•ผ ํ•˜๋Š” ๊ฒƒ์ด๊ณ , Android์™€ ๊ฐ™์ด USB ๊ผฝ์ž๋งˆ์ž ํŒŒ์ผ ๊ด€๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์—†๋Š” ๋ถˆํŽธํ•จ์˜ ์ด์œ ๊ฐ€ ๋œ๋‹ค.

App Sandbox โ˜ Apple Developer Documentation

 

์ฆ‰, SandBox๋Š” ํŒŒ์ผ, ํ™˜๊ฒฝ์„ค์ •, ๋„คํŠธ์›Œํฌ ๋ฆฌ์†Œ์Šค, ํ•˜๋“œ์›จ์–ด์— ๋Œ€ํ•œ ์•ฑ์˜ ์ ‘๊ทผ์„ ์ œํ•œํ•˜๋Š” ์„ธ๋ถ„ํ™”๋œ ์ œ์–ด ์ง‘ํ•ฉ์ด๋ฉฐ,

iOS๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๊ฐ๊ฐ์˜ ์•ฑ์ด ํ•˜๋‚˜์˜ SandBoxํ™” ๋˜์–ด ์žˆ๋‹ค.


2. SandBox ๊ตฌ์กฐ

์ƒˆ๋กœ์šด ์•ฑ์ด ๋””๋ฐ”์ด์Šค ๋‚ด์— ์„ค์น˜๋˜๋Š” ๋™์•ˆ ์ธ์Šคํ†จ๋Ÿฌ๋Š” ํ•ด๋‹น ์•ฑ์˜ Sandbox ๋‚ด์— ์—ฌ๋Ÿฌ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ฐ๊ฐ์˜ ์ปจํ…Œ์ด๋„ˆ๋Š”  ๊ฐ์ž์˜ ์—ญํ• ์ด ์กด์žฌํ•œ๋‹ค.

 

์•„๋ž˜ ๊ทธ๋ฆผ์€ ์•ฑ์˜ ์ƒŒ๋“œ๋ฐ•์Šค ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ์ด๋‹ค. ๋ณด์•ˆ์„ ์œ„ํ•ด iOS ์•ฑ๊ณผ ํŒŒ์ผ ์‹œ์Šคํ…œ์˜ ์ƒํ˜ธ ์ž‘์šฉ์€ ์•ฑ์˜ sandbox ๋””๋ ‰ํ† ๋ฆฌ์—์žˆ๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ œํ•œ๋œ๋‹ค.

์ด๋Ÿฌํ•œ ๊ตฌ์กฐ๋Š” iOS์—์„œ ์•ฑ ๊ตฌ๋™์‹œ ํ•„์š”ํ•œ ํŒŒ์ผ๋“ค์„ ์ €์žฅํ•ด ๋†“๋Š” ๊ตฌ์กฐ๋กœ, ์ž„์˜์ ์œผ๋กœ ์œ ์ €๋“ค์ด ์ ‘๊ทผ, ์ƒ์„ฑ, ํŽธ์ง‘ ๋ถˆ๊ฐ€ํ•˜๋‹ค.

 

SandBox๋Š” Bundle ContainerData ContaineriCloud Container ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๊ณ , ์ฝ”๋“œ๋กœ ๊ฐ Container ๊ฒฝ๋กœ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

๊ฐ Container ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์•„๋ž˜์—~

// ์•ฑ ์„ค์น˜ ๊ฒฝ๋กœ
print(NSHomeDirectory())
// tmp directory
let tmpDirURL = FileManager.default.temporaryDirectory
// library directory
let LibDirURL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first
// document directory
let DocDirURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first

 

[๊ฒฐ๊ณผ]

1) ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ

  • ์„ค์น˜ ๊ฒฝ๋กœ ํ™•์ธ

  • container ๊ฒฝ๋กœ ํ™•์ธ
file:///private/var/mobile/Containers/Data/Application/E6356173-319D-4CD7-B93A-AD99ACBF6C9A/tmp/
file:///var/mobile/Containers/Data/Application/E6356173-319D-4CD7-B93A-AD99ACBF6C9A/Library/
file:///var/mobile/Containers/Data/Application/E6356173-319D-4CD7-B93A-AD99ACBF6C9A/Documents/

 

2) ์‹ค์ œ ๋‹จ๋ง

  • ์„ค์น˜ ๊ฒฝ๋กœ ํ™•์ธ
    • /var/mobile/Containers/Data/Application/8B0321D1-8FF4-4610-8DCF-FC7412E30CCC
    • ์šฐ๋ฆฌ๊ฐ€ ๋ฌผ๋ฆฌ์  ์œ„์น˜ ์•Œ ์ˆ˜ ์—†์–ด ์ ˆ๋Œ€์  ๊ฒฝ๋กœ read๋งŒ api๋กœ ๊ฐ€๋Šฅ(write, delete, backup ์•ˆ๋ผ)

์‹ค์ œ ์•ฑ์˜ ๊ตฌ์กฐ๋ฅผ ๋ณด๊ณ  ์‹ถ์œผ๋ฉด, Xcode๋ฅผ ํ†ตํ•ด ์„ค์น˜๋œ ์•ฑ์˜ ํŒจํ‚ค์ง€ ๋‚ด์šฉ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

Build Target > Manage Run Destinations... > ๋นŒ๋“œํ•œ ์•ฑ ์„ ํƒ > Download Container ํด๋ฆญ!

๋‹ค์šด๋กœ๋“œ ๋ฐ›์€ ๊ฒฝ๋กœ์—์„œ ํ•ด๋‹น ์•ฑ์˜ ํŒจํ‚ค์ง€ ๋‚ด์šฉ ํ™•์ธ ๊ฐ€๋Šฅ!


1. Bundle Container

  • ์•ฑ์˜ Bundle(AppName.app)์„ ๋ณด์œ ํ•œ๋‹ค.
    • Bundle(๋ฒˆ๋“ค)์ด๋ž€?
      • ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ํŒŒ์ผ๊ณผ info.plist, ํ•„์š”ํ•œ ๋ฆฌ์†Œ์Šค(assets, string ๋“ฑ)๋ฅผ ํ•จ๊ป˜ ๋ฌถ์€ ๋””๋ ‰ํ† ๋ฆฌ
      • ๋ฆฌ์†Œ์Šค๋Š” ํ”„๋กœ์ ํŠธ ๋‚ด build phases > copy bundle resources ๋กœ ์ถ”๊ฐ€ํ•œ ํŒŒ์ผ๋“ค(compile bundle sources๊ฐ€ ๋ฐ”์ด๋„ˆ๋ฆฌ ํ˜•ํƒœ์˜ ์‹คํ–‰ํŒŒ์ผ ์ €์žฅ๋จ)
      • ๋‹จ์ˆœํžˆ ํŒŒ์ผ์˜ ์œ„์น˜๋ฅผ ์ด๋™์‹œํ‚ด์œผ๋กœ์จ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด๋‚˜ ์†Œํ”„ํŠธ์›จ์–ด์˜ ์ผ๋ถ€๋ถ„์„ ์„ค์น˜ํ•˜๊ฑฐ๋‹ˆ ์ด์ „ํ•˜๋Š”๋ฐ ํŽธ๋ฆฌํ•จ์„ ์ œ๊ณต
      • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ์ €์žฅ
      • ์ž„์˜์˜ ์กฐ์ž‘์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์•ฑ์ด ์„ค์น˜๋˜๋Š” ๋™์•ˆ ์ด ๋””๋ ‰ํ† ๋ฆฌ๋Š” signed ๋œ๋‹ค.(read-only)
  • read-only์˜ ๊ถŒํ•œ์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ณ , ์ด ๋””๋ ‰ํ† ๋ฆฌ์˜ ๋‚ด์šฉ์€ iCloud๋‚˜ iTunes์— ๋ฐฑ์—…๋˜์ง€ ์•Š๋Š”๋‹ค.
    • bundle ๊ฒฝ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ
let BundleURL = Bundle.main.bundleURL

1) ๋‹จ๋ง๊ธฐ

 

file:///private/var/containers/Bundle/Application/0255A099-08E7-409B-BC06-F358F0251463/Ruti.app/

 

2) ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ

file:///Users/leeyeon2/Library/Developer/CoreSimulator/Devices/151D065C-A8DA-4E99-BC5E-1948BEE32DEC/data/Containers/Bundle/Application/34356091-9B59-408F-A365-9A228FF6150D/Ruti.app/

2. Data Container

Data ์ปจํ…Œ์ด๋„ˆ๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ์‚ฌ์šฉ์ž๋ฅผ ์œ„ํ•œ Data๋ฅผ ๋‹ด๊ณ ์žˆ๊ณ ,

Data ์ปจํ…Œ์ด๋„ˆ๋Š” ๋ช‡๊ฐœ์˜ ์„œ๋ธŒ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค.

  • Realm Database๋Š” Document ๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๋งŒ์•ฝ ์™ธ๋ถ€์— ๋…ธ์ถœ๋˜๋ฉด ์•ˆ๋˜๋Š” ์ค‘์š”ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด Library์˜ Application Support ํด๋”๋กœ ๊ฒฝ๋กœ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

 

 

โœ”๏ธ Documents

  • ์•ฑ์„ ํ†ตํ•ด ์ƒ์„ฑ๋œ ๋ฌธ์„œ๋‚˜ ๋ฐ์ดํ„ฐ ๋“ฑ์„ ์ €์žฅํ•œ๋‹ค.
  • ์‚ฌ์šฉ์ž๊ฐ€ ์ˆ˜์ •/์ถ”๊ฐ€/์‚ญ์ œ ๊ฐ€๋Šฅ(read-write ๊ฐ€๋Šฅ, ๊ฐœ๋ฐœ์ž๊ฐ€ ์›ํ•œ๋‹ค๋ฉด ํŠน์ • ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋งŒ ์ ‘๊ทผ์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค.)
  • ์‚ฌ์šฉ์ž์—๊ฒŒ ๋…ธ์ถœ๋˜๋Š” ํŒŒ์ผ๋งŒ ์ €์žฅํ•ด์•ผ ํ•˜๊ณ , ๋‚ด๋ถ€์˜ ํŒŒ์ผ๋“ค์€ iCloud/iTunes ๋ฐฑ์—…๋œ๋‹ค.

 

โœ”๏ธ Library

  • ์œ ์ € ๋ฐ์ดํ„ฐ ํŒŒ์ผ ๋ฐ ์ž„์‹œ ํŒŒ์ผ์„ ์ œ์™ธํ•œ ๋ชจ๋“  ํŒŒ์ผ์„ ๊ด€๋ฆฌํ•œ๋‹ค.
  • ์‚ฌ์šฉ์ž์—๊ฒŒ ์ง์ ‘ ๋…ธ์ถœ ์—†์Œ
  • ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ ๊ฐ€๋Šฅ
  • ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฌ 
    • Caches : ๋ฐ˜๋ณต ์‚ฌ์šฉ ์ž„์‹œ ๋ฐ์ดํ„ฐ, ์•ฑ์˜ ์Šค๋ƒ…์ƒท ๋“ฑ์ด ์ €์žฅ (+ ํ‚นํ”ผ์…”๋ฅผ ์ด๋ฏธ์ง€๋„ ์บ์‹ฑ๋˜์–ด ์ €์žฅ๋œ๋‹ค.)
    • Application Support : ์•ฑ์ด ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ, ์„ค์ •, ๋ฆฌ์†Œ์Šค ๋“ฑ์ด ์ €์žฅ๋จ, ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ง€์ง€๋Š” ์•Š์ง€๋งŒ App ์—์„œ ์‚ฌ์šฉ์ž ๊ธฐ๋ก์„ ๋‚จ๊ธฐ๋Š” ๊ณต๊ฐ„
    • Preferences : ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™˜๊ฒฝ์„ค์ • ๋ฐ์ดํ„ฐ ์ €์žฅ, UserDefaults, CFPreferences ๋“ฑ ์ €์žฅ

 

โœ”๏ธ tmp

  • ์•ฑ์— ์žฌ์‚ฌ์šฉ์ด ๋˜์ง€ ์•Š์œผ๋ฉฐ ์žฅ๊ธฐ๊ฐ„ ์œ ์ง€ํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ์ผ์‹œ์ ์œผ๋กœ ํ•„์š”ํ•œ ํŒŒ์ผ์„ ์ €์žฅ
  • ์‹œ์Šคํ…œ์€ ์•ฑ์ด ์‹คํ–‰๋˜๊ณ  ์žˆ์ง€ ์•Š์„ ๋•Œ ์ด๋Ÿฌํ•œ ํŒŒ์ผ์„ ์ฃผ๊ธฐ์ ์œผ๋กœ ์‚ญ์ œํ•˜์—ฌ ๋””๋ฐ”์ด์Šค ๊ณต๊ฐ„์„ ๋‚ญ๋น„ํ•˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค.

3. iCloud Container

  • ๋Ÿฐํƒ€์ž„์— ์ ‘๊ทผ์„ ์š”์ฒญํ•  ์ˆ˜ ์žˆ๋Š” ์ถ”๊ฐ€ ์ปจํ…Œ์ด๋„ˆ ๋””๋ ‰ํ† ๋ฆฌ.
728x90
๋ฐ˜์‘ํ˜•