首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法显示实时数据,需要反复打开应用程序才能从Fiebase数据库加载新数据

问无法显示实时数据,需要反复打开应用程序才能从Fiebase数据库加载新数据
EN

Stack Overflow用户
提问于 2019-05-21 02:13:51
回答 1查看 17关注 0票数 0

我正在将用户输入数据加载到此屏幕上,但只有在打开应用程序之前输入的数据才会显示,用户输入的新数据不会显示,直到我们重新打开应用程序。

这是我的代码,使用来自数据库的数据显示,但不是用户所做的最新更新。

代码语言:javascript
复制
constructor(){
    super()
    this.state ={
        persons:[]
    }
}

renderItem =({item}) => {
    const start = 'mailto:';
    let mail= item.email;
    const end = '?subject=Application for Job';
    const add = start.concat(mail,end);
    console.log(item.email);
    return(
        <View style={{fles:1, flexDirection:'column',marginBottom:5,marginLeft:5,marginRight:5, borderRadius: 4, borderWidth: 0.5, borderColor: '#6d7ba5',}}>
            <Text style={styles.text}>Job Title:  {item.JobTitle}</Text>
            <Text style={styles.text2}>Job Description:  {item.JobDesc}</Text>
            <Text style={styles.text2}>Working Hours:  {item.Timing}</Text>
            <Text style={styles.text2}selectable={true}
            onPress={() => Linking.openURL(add)}>Payment:  {item.Payment}</Text>
            <Text style={styles.text2} selectable={true}
            onPress={() => Linking.openURL(add)}>Contact Info:  {item.Number}</Text>
            <Text style={styles.text2} selectable={true}
            onPress={() => Linking.openURL(add)} >Email:  {item.Email}</Text>
            <Text style={styles.text2} selectable={true}
            onPress={() => Linking.openURL(add)}>Remark:  {item.Remark}</Text>
        </View>
    )

}
componentDidMount(){
    const url ='https://parttimer-88182.firebaseio.com/users.json';

    axios.get(url).then(res =>{
        const persons = res.data;
        console.log(Object.entries(persons));
        this.setState({persons});
    }); 


}


refreshHandler(){
    const url ='https://parttimer-88182.firebaseio.com/users.json';

    axios.get(url).then(res =>{
        const persons = res.data;
        console.log(Object.entries(persons));
        this.setState({persons});
    }); 


}
render() {
    return (
        <SafeAreaView style={{ flex: 1 }}>
            <ImageBackground source ={require('../../assets/Background/back2.jpg')} style={{width:'100%', height:'100%'}}>
            <View style={styles.container}>

                <Header style={{height:65,backgroundColor:'transparent'}}>                          
                    <Icon name='angle-double-right' style={styles.icon} onPress={() => this.props.navigation.navigate('openDrawer')} />
                    <Text style={{fontWeight: 'bold', color:'black' ,fontSize:30,marginTop:20 }} >Explore Adds</Text>
                    <Icon name='info-circle' style={styles.icon2} onPress={this._simpleAlertHandler} />
                </Header>

                <Text style={{ fontSize: 24, fontWeight: '700',marginTop:5, paddingHorizontal: 20 ,backgroundColor:'rgba(217,224,244,0.7)'}}>
                            Select your desired job here !
                </Text>

                <View style={{ height:130,backgroundColor:'rgba(217,224,244.0.7)',borderRadius: 4, borderWidth: 0.5, borderColor: '#6d7ba5'}}>
                    <ScrollView horizontal={true} showsHorizontalScrollIndicator={false}>
                        <Category 
                            imageUri={require('../../assets/experiences.jpg')}
                            name="Janta Travels"/>
                        <Category 
                            imageUri={require('../../assets/rajat.jpg')}
                            name="HistoryGuider" />
                        <Category 
                            imageUri={require('../../assets/army.jpg')}
                            name="Army Trainers"/>
                        <Category 
                            imageUri={require('../../assets/experiences.jpg')}
                            name="Job 1"/>

                    </ScrollView>
                </View>
                <View style={{flex:1,marginTop:5}}> 
                    <FlatList
                        data={Object.entries(this.state.persons).map(item =>(item[1]))}
                        renderItem={this.renderItem}
                    />
                    <View style={{height:30}} >
                    <Button title="    REFRESH    " onPress={this.refreshHandler}
                    color='#841585' borderRadius={50} />

                    </View>
                </View>

            </View>
            </ImageBackground>
    </SafeAreaView>        
    );
}

}

谁能告诉我如何加载用户最近输入的内容。

EN

回答 1

Stack Overflow用户

发布于 2019-05-21 05:44:14

您正在使用Firebase实时数据库的REST API,这意味着当您调用axios.get(url)时,数据只获取一次。

如果您希望从Firebase获得实时更新,请考虑使用Firebase SDK来实现此目的。例如,Firebase实时数据库的React Native Firebase SDK非常成熟,它使用Firebase的本地有线协议来监听实时更新。要开始使用这方面的API,请看一下rnfirebase.io和Firebase documentation for web developers。

如果你真的坚持不使用SDK,你可以看看Firebase的REST streaming protocol,它通过REST API提供实时更新。只需注意,您可能会发现自己以这种方式在自己的代码中重新创建了Firebase SDK的重要部分。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56226162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档