4T2F / ThinkBig

🌟씽크빅 스터디🌟
5 stars 1 forks source link

iOS 앱의 생명주기(App Life Cycle)에 대해 설명해주세요. #36

Open JooYoungNoh opened 6 months ago

JooYoungNoh commented 6 months ago

🔎 앱 생명주기(App's Life Cycle)란?

🪡 앱의 생명주기 구조

Not Running

Foreground

Active

Inactive

background

Running

Suspended

🧑‍🔧 앱의 생명주기 관리

SwiftUI

@Environment(\.scenePhase) private var scencePhase

    @State private var color: Color = .white
    @State private var text: String = ""

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(color)
            Text(text)
        }
        .padding()
        .onChange(of: scencePhase) {
            switch scencePhase {
            case .active:
                color = .red
                text = "Active"
            case .inactive:
                color = .blue
                text = "Inactive"
            case .background:
                color = .yellow
                text = "Background"
            @unknown default:
                print("Unknown scenephase")
            }
        }
    }



UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    let viewModel = TestViewModel.shared

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let _ = (scene as? UIWindowScene) else { return }
        print("앱 실행 예정")
    }

    func sceneDidDisconnect(_ scene: UIScene) {
        print("앱 끔")
    }

    // Inactive_In -> Active
    func sceneDidBecomeActive(_ scene: UIScene) {
        viewModel.text = "Active"
        viewModel.color = .systemRed
        print("Active")
    }

    // Active >> Inactive_Out
    func sceneWillResignActive(_ scene: UIScene) {
        viewModel.text = "Inactive_Out"
        viewModel.color = .systemBlue
        print("Inactive_Out")
    }

    // Background >> Inactive_In
    // 앱 실행 예정 >> Inactive_In
    func sceneWillEnterForeground(_ scene: UIScene) {
        viewModel.text = "Inactive_In"
        viewModel.color = .systemGreen
        print("Inactive_In")
    }
    // Inactive_Out >> Background
    func sceneDidEnterBackground(_ scene: UIScene) {
        viewModel.text = "Background"
        viewModel.color = .systemYellow
        print("Background")
    }
}
import Combine
import SnapKit

import UIKit

class TestViewModel: ObservableObject{
    static let shared = TestViewModel()

    @Published var color: UIColor = .black
    @Published var text: String = ""
}

class ViewController: UIViewController {
    var disposalbleBag = Set<AnyCancellable>()

    let viewModel = TestViewModel.shared

    lazy var textLabel: UILabel = {
        let label = UILabel()
        label.textColor = .black
        label.font = .systemFont(ofSize: 20)
        label.layer.cornerRadius = 10
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setView()
        setBinding()
    }

    func setBinding() {
        self.viewModel.$text.sink{ text in
            print(text)
            self.textLabel.text = "앱 생명주기 상태: \(text)"
        }.store(in: &disposalbleBag)

        self.viewModel.$color.sink{ color in
            self.textLabel.textColor = color
        }.store(in: &disposalbleBag)
    }
}

//MARK: 뷰 그리기
extension ViewController {
    func setView() {
        self.view.addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalToSuperview().offset(-50)
            make.height.equalTo(40)
        }
    }
}



Hsungjin commented 6 months ago

Background의 하위개념이 Suspended 으로 이해를 했는데 이렇게 되면 Background에서 할수있는 일들 또한 Suspended에서 수행할수 있는건가요?

ha-nabi commented 6 months ago

앱이 백그라운드 상태일 때 수행할 수 있는 작업과 제한 사항이 있을까요? 예를 들어 백그라운드에서 위치 추적, 데이터 다운로드, 음악 재생 같은건 어떻게 처리할 수 있는지 궁금합니다

withseon commented 6 months ago

Suspended 설명 중 '최근 작업을 빠르게 로드하기 위해서 메모리에 관련 데이터'에는 무엇이 있을지 궁금합니다.

bdrsky2010 commented 6 months ago

suspend와 running 상태가 background에 포함되어 보이는데 이 두 상태의 처리는 background 상태일 때 어떤식으로 로직을 수행할 수 있는지 궁금해요

JooYoungNoh commented 5 months ago

Background의 하위개념이 Suspended 으로 이해를 했는데 이렇게 되면 Background에서 할수있는 일들 또한 Suspended에서 수행할수 있는건가요?


앱이 백그라운드 상태일 때 수행할 수 있는 작업과 제한 사항이 있을까요? 예를 들어 백그라운드에서 위치 추적, 데이터 다운로드, 음악 재생 같은건 어떻게 처리할 수 있는지 궁금합니다.


Suspended 설명 중 '최근 작업을 빠르게 로드하기 위해서 메모리에 관련 데이터'에는 무엇이 있을지 궁금합니다.


suspend와 running 상태가 background에 포함되어 보이는데 이 두 상태의 처리는 background 상태일 때 어떤식으로 로직을 수행할 수 있는지 궁금해요