{"id":2977,"date":"2025-12-04T09:37:00","date_gmt":"2025-12-04T00:37:00","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=2977"},"modified":"2025-12-04T11:37:45","modified_gmt":"2025-12-04T02:37:45","slug":"swiftobservedobject","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/12\/04\/swiftobservedobject\/","title":{"rendered":"[Swift]ObservedObject and StateObject"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc2a4\uc704\ud504\ud2b8\uc5d0\uc11c @ObservedObject\uc640 @StateObject\uc5d0 \ub300\ud55c \uc124\uba85\uc785\ub2c8\ub2e4.<br>Here&#8217;s an explanation of @ObservedObject and @StateObject in Swift.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb @ObservedObject <br>\u2714\ufe0f @ObservedObject\ub294 \uad6c\uc870\uccb4\uc758 \ucd08\uae30\ud654 \uc2dc\uc810\uc5d0 \uacc4\uc18d\ud574\uc11c \uc778\uc2a4\ud134\uc2a4(\ud504\ub85c\ud37c\ub2c8 \ucd08\uae30\ud654)\ub97c \ub2e4\uc2dc \uc2e4\ud589 \ud569\ub2c8\ub2e4.<br>@ObservedObject continually re-executes the instance (property initialization) at the time of struct initialization.<\/p>\n\n\n\n<p>\u2714\ufe0f @ObservedObject\ub294 \ubd80\ubaa8\ubdf0\uc5d0\uc11c \uc804\ub2ec\ubc1b\uc740 \ubdf0 \ubaa8\ub378\uc744 \uad00\ucc30\ub9cc \ud569\ub2c8\ub2e4.<br>(\uad00\ucc30\uc774\ub780 \ubdf0\ubaa8\ub378[Class UserSettings] \uac12\uc774 \ubcc0\ud558\uba74 [@Published\ub85c \uc120\uc5b8\ub41c \ubcc0\uc218\uc758 \uac12])UI\ub97c \uc5c5\ub370\uc774\ud2b8 \ud558\ub294\uac83\uc744 \uc758\ubbf8\ud569\ub2c8\ub2e4.)<br>@ObservedObject only observes the view model passed from the parent view. (Observation means that when the value of the view model [class UserSettings] changes, the UI is updated [the value of the variable declared with @Published].)<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb @StateObject<br>\u2714\ufe0f@StateObject\ub294 SwiftUI\uc758 state storage(\ubdf0\uc758 \ub77c\uc774\ud504 \uc2f8\uc774\ud074 \ub3d9\uc548 \ubcf4\uad00\ub418\ub294 \uc800\uc7a5\uc18c)\uc5d0 <br>\uc778\uc2a4\ud134\uc2a4\ub97c \uc800\uc7a5\ud558\uace0, \ucc98\uc74c \ubdf0\uac00 \uc0dd\uc131\ub420 \ub54c\ub9cc \ucd08\uae30\ud654\ud558\ub3c4\ub85d \ubcf4\uc7a5\ud569\ub2c8\ub2e4.<br>@StateObject stores an instance in SwiftUI&#8217;s state storage (which is kept throughout the view&#8217;s lifecycle) and ensures that it is initialized only when the view is first created.<\/p>\n\n\n\n<p>\u2714\ufe0f @StateObject\ub294 \ubdf0\ubaa8\ub378\uc758 \uc778\uc2a4\ud134\uc2a4\ub97c \uc0dd\uc131\ud558\uace0 \uad00\ub9ac\ud558\uba70 \ubd80\ubaa8\ubdf0\uc5d0\uc11c \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<br>@StateObject creates and manages an instance of the view model and uses it in the parent view.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc544\ub798\uc758 \ucf54\ub4dc\ub97c \uc2e4\ud589\ud574\ubcf4\uba74 \ucc28\uc774\ub97c \ud655\uc778 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>You can see the difference by running the code below.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb@StateObject\ub97c \uc0ac\uc6a9\ud560 \uc704\uce58\uc5d0 @ObservedObject\ub97c \uc0ac\uc6a9\ud558\uba74 init()\uc774 \ub450\ubc88 \uc2e4\ud589\ub418\ub294 \ubb38\uc81c\uac00 \ubc1c\uc0dd\ud569\ub2c8\ub2e4.<br>If you use @ObservedObject where you would use @StateObject, you will run into the problem that init() is executed twice.<\/p>\n\n\n\n<p>\ud83d\udc49\uadf8\ub798\uc11c @StateObject\ub97c \uc0ac\uc6a9\ud558\uba74 \uc774\ub7f0 \ubb38\uc81c\ub97c \ubc29\uc9c0 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>So using @StateObject can help us avoid this problem.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb\ucf54\ub4dc\/code<br>\u2714\ufe0f ContentView.swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport SwiftUI\nimport Combine \/\/ add Combine\n\n\nclass MyViewModel: ObservableObject {\n    @Published var data = \"\"\n    \n    init() {\n        print(\"MyViewModel init\")\n        fetchData() \/\/ \ub124\ud2b8\uc6cc\ud06c \ud638\ucd9c \uac19\uc740 \uc791\uc5c5\n    }\n    func fetchData() {\n        print(\"fetchData called\")\n        data = \"Data Updated...\"\n    }\n}\n\nstruct ContentView: View {\n@State private var count = 0\n   @ObservedObject var vm = MyViewModel() \/\/ \uc624\ub958 \/ error\n    \/\/@StateObject var vm = MyViewModel() \/\/ \uc815\uc0c1 \/ normal\n    \n    var body: some View {\n        VStack {\n            Image(systemName: \"globe\")\n                .imageScale(.large)\n                .foregroundStyle(.tint)\n            Text(\"Data:\\(vm.data)\")\n            \n            Button(\"Update view\") {\n                count += 1\n                print(count)\n                vm.fetchData()\n            }\n            Button(\"Update Clear\") {\n                count = 0\n                vm.data = \"Clear\"\n                print(count)\n            }\n            \n        }\n        .padding()\n    }\n}\n\n#Preview {\n    ContentView()\n}\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f RootView.swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import SwiftUI\n\nstruct RootView: View {\n    var body: some View {\n        NavigationStack {\n            VStack(spacing: 20) {\n                NavigationLink(\"Go to ContentView again\") {\n                    ContentView()   \/\/ \ubdf0 \uc7ac\uc0dd\uc131 \/ Regenerate view\n                }\n\n                ContentView()      \n                    .padding()\n            }\n            .navigationTitle(\"RootView\")\n        }\n    }\n}\n\n#Preview {\n    RootView()\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc2a4\ud06c\ub9b0 \uc0f7 \/ ScreenShot<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"[Swift] @ObservedObject and @StateObject\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/UYBRaWuF8Ow?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc2a4\uc704\ud504\ud2b8\uc5d0\uc11c @ObservedObject\uc640 @StateObject\uc5d0 \ub300\ud55c \uc124\uba85\uc785\ub2c8\ub2e4.Here&#8217;s an explanation of @ObservedObject and @StateObject in Swift. \ud83d\udc49\ud83c\udffb @ObservedObject \u2714\ufe0f @ObservedObject\ub294 \uad6c\uc870\uccb4\uc758 \ucd08\uae30\ud654 \uc2dc\uc810\uc5d0 \uacc4\uc18d\ud574\uc11c \uc778\uc2a4\ud134\uc2a4(\ud504\ub85c\ud37c\ub2c8 \ucd08\uae30\ud654)\ub97c \ub2e4\uc2dc \uc2e4\ud589 \ud569\ub2c8\ub2e4.@ObservedObject continually re-executes the instance (property initialization) at the time of struct initialization. \u2714\ufe0f @ObservedObject\ub294 \ubd80\ubaa8\ubdf0\uc5d0\uc11c \uc804\ub2ec\ubc1b\uc740 \ubdf0 \ubaa8\ub378\uc744 \uad00\ucc30\ub9cc \ud569\ub2c8\ub2e4.(\uad00\ucc30\uc774\ub780 \ubdf0\ubaa8\ub378[Class UserSettings] \uac12\uc774 \ubcc0\ud558\uba74 [@Published\ub85c \uc120\uc5b8\ub41c \ubcc0\uc218\uc758 \uac12])UI\ub97c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,1],"tags":[],"class_list":["post-2977","post","type-post","status-publish","format-standard","hentry","category-swift","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=2977"}],"version-history":[{"count":21,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2977\/revisions"}],"predecessor-version":[{"id":3000,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2977\/revisions\/3000"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=2977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=2977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=2977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}