[Swift]ObservedObject and StateObject – 1

๐Ÿ‘‰๐Ÿป ์Šค์œ„ํ”„ํŠธ์—์„œ ObservedObject์™€ StateObject๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
This is a simple way to use ObservedObject and StateObject in Swift.

โœ”๏ธ @StateObject์™€ย @ObservedObject๋Š” ๋‘˜๋‹ค ๋ทฐ์— ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์ง€๋งŒ, ๋‘˜์€ ์•ฝ๊ฐ„์˜ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
Both @StateObject and @ObservedObject are methods for binding to a view, but there are slight differences between the two.

โœ”๏ธ @StateObject

— ์ผ๋ฐ˜์ ์œผ๋กœ ๋ทฐ์—์„œ ์ง์ ‘ ์ƒํƒœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
It is generally used when creating and managing state objects directly in the view.

— ๋ทฐ๊ฐ€ ์ฒ˜์Œ ์ƒ์„ฑ๋  ๋•Œ๋งŒ ํ•ด๋‹น ObservableObject ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , ๋ทฐ๊ฐ€ ๋‹ค์‹œ ๋ Œ๋”๋ง๋˜๊ฑฐ๋‚˜ ๊ฐฑ์‹ ๋  ๋•Œ๋Š” ๊ธฐ์กด ์ธ์Šคํ„ด์Šค๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.
Initialize the ObservableObject instance only when the view is first created, and maintain the existing instance when the view is re-rendered or updated.

— ๋ทฐ์—์„œ ์ง์ ‘ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ํ• ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
It is used when managing state directly in the view.

— ์•„๋ž˜์˜ ์ฝ”๋“œ๊ฐ€ ์ด ๋ถ€๋ถ„์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
The code below corresponds to this part.

โœ”๏ธ @ObservedObject

— ์™ธ๋ถ€์—์„œ ์ด๋ฏธ ์ƒ์„ฑ๋œ ObservableObject๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
It receives and uses an ObservableObject that has already been created externally.

— ๋ทฐ๊ฐ€ ๋‹ค์‹œ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ObservableObject ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ณ„์† ์ƒˆ๋กœ ์ƒ์„ฑ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
There may be cases where new ObservableObject instances are created every time the view is recreated.

— ๊ทธ๋ž˜์„œ ๋ถ€๋ชจ๋ทฐ๋ฅผ ์ž์‹๋ทฐ์— ์ „๋‹ฌํ• ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
So, it is used when passing a parent view to a child view.

๐Ÿ‘‰๐Ÿป ์ฝ”๋“œ / Code


import SwiftUI
import Combine


class UserSettings: ObservableObject {
  @Published var username = "Anonymous"
}

struct ContentView: View {
    
    // @StateObject๋Š” SwiftUI์˜ state storage(๋ทฐ์˜ ๋ผ์ดํ”„ ์‹ธ์ดํด ๋™์•ˆ ๋ณด๊ด€๋˜๋Š” ์ €์žฅ์†Œ)์—
    // ์ธ์Šคํ„ด์Šค๋ฅผ ์ €์žฅํ•˜๊ณ , ์ฒ˜์Œ ๋ทฐ๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งŒ ์ดˆ๊ธฐํ™”ํ•˜๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.
    // @StateObject stores an instance in SwiftUI's state storage (which is kept throughout the view's lifecycle) and ensures that it is initialized only when the view is first created.
    
    @StateObject var settings = UserSettings()
    
    // @ObservedObject๋Š” ๊ตฌ์กฐ์ฒด์˜ ์ดˆ๊ธฐํ™” ์‹œ์ ์— ๊ณ„์†ํ•ด์„œUserSettings()๋ฅผ ๋‹ค์‹œ ์‹คํ–‰ ํ•ฉ๋‹ˆ๋‹ค.
    // @ObservedObject will continually re-run UserSettings() at the time of initialization of the struct.
    
    //@ObservedObject var settings = UserSettings()
    
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
            
                .foregroundStyle(.tint)
            Text("User name:\(settings.username)")
                .padding()
            
            Button("John Doe"){
                settings.username = "Jhon Doe"
            }
            Button("Anonymous"){
                settings.username = "Anonymous"
            }
            
            Text("Child View")
        }
        .padding()
    }
}


#Preview {
    ContentView()
}

โœ”๏ธ ์Šคํฌ๋ฆฐ ์ƒท / ScreenShot

Leave a Reply