파이어베이스 : 서버리스를 제공해주는 서비스
동작 방식
서버가 정한 규칙에 따라 대화 요청(Request)
정한 규칙에 따라 요청을 하면 응답(Response)을 수신
서버가 정한 규칙 = API (Application Programming Interface)
규칙 형태는 도메인 / 함수 등 다양한 형태
// 도메인 형식
www.sparta.com/getdata // 데이터 조회 API
www.sparta.com/setdata // 데이터 저장 API
// 함수 형식
db.ref('/like/').on('value') // 데이터 조회 API
db.ref('/like/').on(new_like) // 데이터 저장 API
서버에서 앱에 데이터 전송 시 JSON 형태
JSON 형태 : 리스트와 딕셔너리의 복합 구조
데이터 준비 시점
useEffect(()=>{
//서버 API 사용
//이 화면에서 사용 할 데이터 준비 등
},[])
앱 위치 정보 권한 설정
expo install expo-location
라이브러리
import * as Location from 'expo-location';
위치 가져오기
const getLocation = async () => {
// 외부 API 요청 작업은 try / catch 사용
try {
// 함수 실행 순서 지정 asyns / await
await Location.requestPermissionsAsync()
const locationData = await Location.getCurrentPositionAsync()
} catch (error) {
Alert.alert("위치를 찾을 수 없습니다.", "다시 실행해주세요.")
}
}
useEffect(() => {
setTimeout(() => {
navigation.setOptions({
title: '나만의 꿀팁'
})
let tip = data.tip
setState(tip)
setCateState(tip)
setReady(false)
getLocation()
}, 1000)
}, [])
const func = async function() {
await function01()
await function02()
}
const func = async () => {
await function01()
await function02()
}
async function func() {
await function01()
await function02()
}
axios : 서버가 제공하는 도메인 형식의 API를 사용하기 위한 도구
yarn add axios
라이브러리
import axios from 'axios';
const [weather, setWeather] = useState({
temp: 0,
condition: ''
})
const getLocation = async () => {
try {
await Location.requestPermissionsAsync()
const locationData = await Location.getCurrentPositionAsync()
const latitude = locationData['coords']['latitude']
const longitude = locationData['coords']['longitude']
const API_KEY = "cfc258c75e1da2149c33daffd07a911d"
const result = await axios.get(`http://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${API_KEY}&units=metric`)
const temp = result.data.main.temp
const condition = result.data.weather[0].main
setWeather({
temp, condition
})
} catch (error) {
Alert.alert("위치를 찾을 수 없습니다.", "다시 실행해주세요.")
}
}
...
<Text style={styles.weather}>오늘의 날씨 : {weather.temp + " °C " + weather.condition}</Text>
...
서버리스 : 서버가 없다는 뜻이 아닌, 서버를 직접 만들 필요가 없는 것을 말한다.
Firebase : 구글에서 만든 서버리스 서비스
Firebase 프로젝트 생성
웹 앱 선택 (호스팅 선택 X)
Firebase 연결 expo 도구 설치
// node 버전 14 이상은 firebase 설치 시 오류 발생
// Node.js 제거 후 v12 재설치 진행 필요
expo install firebase
import firebase from 'firebase/app';
import 'firebase/database';
import 'firebase/storage';
//파이어베이스 사이트에서 봤던 연결정보
// 프로젝트 설정 > SDK 설정 및 구성 > CDN이 아니라 구성 선택 > 복사
const firebaseConfig = {
...
// realtime database 시작 후 url 복사
databaseURL: "..."
}
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig)
}
export const firebase_db = firebase.database()
data.json 업로드
현재 코드 리뷰
// 필요 데이터 가져오기
import data from '../data.json';
...
// 데이터 관리 상태
const [state, setState] = useState([])
// 카테고리에 맞는 데이터 저장 관리 상태
const [cateState, setCateState] = useState([])
...
// useEffect에서 데이터를 실제 상태 관리
useEffect(() => {
navigation.setOptions({
title:'나만의 꿀팁'
})
setTimeout(()=>{
let tip = data.tip;
setState(tip)
setCateState(tip)
getLocation()
setReady(true)
}, 1000)
},[])
Firebase 적용
import {firebase_db} from "../firebaseConfig"
...
useEffect(() => {
snavigation.setOptions({
title: '나만의 꿀팁'
})
firebase_db.ref('/tip').once('value').then((snapshot) => {
let tip = snapshot.val()
setState(tip)
setCateState(tip)
getLocation()
setReady(false)
})
}, [])
card.js > DetailPage.js 데이터 idx 전달
// 기존
<TouchableOpacity style={styles.card} onPress={()=>{navigation.navigate('DetailPage',content)}}>
// 변경
<TouchableOpacity style={styles.card} onPress={() => {navigation.navigate('DetailPage', {idx: content.idx})}}>
useEffect(() => {
navigation.setOptions({
title: route.params.title,
headerStyle: {
backgroundColor: 'white',
shadowColor: 'white'
},
headerTintColor: 'black'
})
const { idx } = route.params
firebase_db.ref('/tip/'+idx).once('value').then((snapshot) => {
let tip = snapshot.val()
setTip(tip)
})
}, [])
찜하기 기능 데이터 구조
사용자마다 고유한 ID 값 생성
expo install expo-constants
import Constants from 'expo-constants';
...
const like = () => {
const user_id = Constants.installationId
firebase_db.ref('/like/' + user_id + '/' + tip.idx).set(tip, function(error) {
Alert.alert('찜하기!')
})
}