iOS스터디/iOS

[iOS] Firebase 파이어베이스

슬라임 통통 2021. 3. 12. 01:15
728x90

Firebase 파이어베이스

 

파이어 베이스 구글에서 제공함.

 

서버 자체를 서비스로 제공함.

서버를 구축하지 않아도 데이터를 저장할 수 있게 한다.

 

파이어베이스를 이용하면 앱을 뚝딱 만들수 있다.

 

<설치>

 

firebase.google.com/docs/database/ios/start?hl=ko

 

iOS에서 설치 및 설정  |  Firebase 실시간 데이터베이스

Firebase 실시간 데이터베이스는 클라우드 호스팅 데이터베이스입니다. 데이터는 JSON으로 저장되며 연결된 모든 클라이언트에 실시간으로 동기화됩니다. Android, iOS, 자바스크립트 SDK로 크로스 플

firebase.google.com

 

Firebase 기능

파이어 베이스 기능: 데이터 저장, 실시간 데이터 동기화, 사용자 인증, 데이터 분석 , A/B Testing ...등등

 

 

 

이러한 기능들을 잘 쓰면 사용자 들에게 여러 테스트를 할 수 있다.

 

 

무료 버전도 꽤 많은 것을 지원한다.

 

 

 

SDK 란?

Software Development Kit  : 소프트웨어 개발 도구

파이어 베이스를 사용하기 위해서 사용하기위한 개발도구들이 미리 만들어져 있으므로 우리는 그걸 설치하고 쓰기만 하면된다.

 

 cocoapods : 외부 라이브러리 관리 모듈

 

 

 

 

Firebase Realtime Database

데이터는 너무 복잡하지 않게 , 깊게 들어가지 않게 구성한다.

 

 

 

데이터 가져오기

func updateLabel(){
    db.child("firstData").observeSingleEvent(of: .value) { snapshot in
        let value = snapshot.value as? String ?? ""
        DispatchQueue.main.async {
            self.firstData.text = value
        }
    }
}

 

데이터 넣기

 

 

- 베이직 타입

func saveBasicTypes(){
    db.child("int").setValue(3)
    db.child("double").setValue(3.5)
    db.child("str").setValue("string value - 여러분 안녕")
    db.child("dict").setValue(["id": "anyID", "age": 10, "city": "seoul"])
}

 

파이어베이스에 저장되었음을 알 수 있다. 

 

 

- 사용자 정의

func saveCustomers(){
    let books = [Book(title: "Good to Great", author: "Someone"), Book(title: "Hackiing Growth", author: "Somebody")]
    let customer1 = Customer(id: "\(Customer.id)", name: "Son", books: books)
    Customer.id += 1
    let customer2 = Customer(id: "\(Customer.id)", name: "Dele", books: books)
    Customer.id += 1
    let customer3 = Customer(id: "\(Customer.id)", name: "Kane", books: books)
    Customer.id += 1
        
    db.child("customers").child(customer1.id).setValue(customer1.toDictionary)
    db.child("customers").child(customer2.id).setValue(customer2.toDictionary)
    db.child("customers").child(customer3.id).setValue(customer3.toDictionary)
}
    
    
struct Customer {
    let id: String
    let name: String
    let books: [Book]
    
    var toDictionary: [String: Any]{
        let booksArray = books.map{ $0.toDictionary }
        let dict: [String: Any] = ["id": id, "name": name, "books": booksArray]
        return dict
    }
    static var id: Int = 0
}

struct Book {
    let title: String
    let author: String
    
    var toDictionary: [String: Any]{
        let dict: [String: Any] = ["title": title, "author": author]
        return dict
    }
}

 

 

728x90
반응형