[240226] TIL

YoungHyun Kim·2024년 2월 26일
1

TIL ✍️

목록 보기
51/68
//
//  ViewController.swift
//  FirestorePractice
//
//  Created by 영현 on 2/26/24.
//

import UIKit
import FirebaseFirestore
import SnapKit

class ViewController: UIViewController {
    
    let db = Firestore.firestore()
    
    let createButton: UIButton = {
        let button = UIButton()
        button.setTitle("데이터 생성", for: .normal)
        button.setTitleColor(UIColor.blue, for: .normal)
        return button
    }()
    
    let readButton: UIButton = {
        let button = UIButton()
        button.setTitle("데이터 읽기", for: .normal)
        button.setTitleColor(UIColor.blue, for: .normal)
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .white
        
        addViews()
        setConstraints()
        addActions()
    }
    
    func addViews() {
        view.addSubview(createButton)
        view.addSubview(readButton)
    }
    
    func setConstraints() {
        createButton.snp.makeConstraints{ make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-20)
        }
        
        readButton.snp.makeConstraints{ make in
            make.centerX.equalToSuperview()
            make.top.equalTo(createButton.snp.bottom).offset(20)
        }
    }
    
    func addActions() {
        createButton.addTarget(self, action: #selector(createPillData), for: .touchUpInside)
        readButton.addTarget(self, action: #selector(readPillData), for: .touchUpInside)
    }
    
    @objc func createPillData() async {
        print("데이터 생성 함수 시작")
        
        do {
            let ref = try await db.collection("pills").addDocument(data: [
                "title" : "게보린",
                "type" : "일반",
                "day" : ["mon", "wed", "fri"]
            ])
            print("데이터 입력 성공")
            print("문서 ID => \(ref.documentID)")
        } catch {
            print("에러 : \(error)")
        }
    }
    
    @objc func readPillData() async {
        print("데이터 읽기 함수 시작")
        
        do {
            let snapshot = try await db.collection("pills").getDocuments()

            for document in snapshot.documents {
                print("\(document.documentID) => \(document.data())")
            }
            print("데이터 읽기 성공")
        } catch {
            print("에러 : \(error)")
        }
    }
}

뭘 안해서인지는 모르겠는데, AppCheck failed: 'The operation couldn’t be completed. The attestation provider DeviceCheckProvider is not supported on current platform and OS version.'라는 에러가 뜸.

나는 단지 Firestore를 사용해서 DB를 만들어 보고 싶었을 뿐입니다. 휴

내일 문제가 해결되면 트러블 슈팅 로그 적겠습니다.

profile
iOS 개발자가 되고 싶어요

0개의 댓글