未输入Firebase onDataChange [英] Firebase onDataChange not entered

查看:191
本文介绍了未输入Firebase onDataChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在setUser方法中,我试图从我的数据库中读取数据,我将使用它创建User,Worker,Manager或Administrator的实例。我也知道以下三行代码是不正确的,但是它们不要提出任何错误,所以现在可以忽略它们。

 字符串名称= mUserDatabase.child(uniqueId).child(name)。orderByValue()。toString(); 
int zip = mUserDatabase.child(uniqueId).child(zipcode)。orderByValue()。hashCode();
String phone = mUserDatabase.child(uniqueId).child(phone number)。orderByValue()。toString();

让我感到困惑的是onDataChange是在onActivityResult中输入的,而不是在setUser中输入的。我不明白为什么onDataChange没有进入setUser。 firebase的引用是正确的,我导航到与typ.toString()记录的URL,它到正确的条目。如果这是相关的,我也设置了公开的权限。我也连接到互联网。我也研究了类似问题的其他答案,这些答案与问题无关。
可能会发生什么,我会感激任何帮助!

pre $ package edu.gatech.cs2340.waterfall。控制器;
导入android.content.Intent;
导入android.support.v7.app.AppCompatActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.View;
导入android.view.animation.Animation;
导入android.view.animation.AnimationUtils;
导入android.widget.ImageView;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.ResultCodes;
导入com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.Arrays;

import edu.gatech.cs2340.waterfall.R;
import edu.gatech.cs2340.waterfall.model.Administrator;
import edu.gatech.cs2340.waterfall.model.Manager;
import edu.gatech.cs2340.waterfall.model.Model;
import edu.gatech.cs2340.waterfall.model.User;
import edu.gatech.cs2340.waterfall.model.Worker;

导入static android.R.attr.data;
import static android.R.attr.dateTextAppearance;
导入静态android.R.attr.type;

公共类WelcomeActivity扩展了AppCompatActivity {

private static FirebaseAuth auth;
private static final int RC_SIGN_IN = 0;
private static DatabaseReference mUserDatabase;
private static DatabaseReference mReportDatabase;
私有静态字符串类型;
public static FirebaseAuth getAuth(){
return auth;


public static DatabaseReference getmUserDatabase(){
return mUserDatabase;


public static DatabaseReference getmReportDatabase(){
return mReportDatabase;


@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
ImageView myImageView =(ImageView)findViewById(R.id.logo);
Animation fade = AnimationUtils.loadAnimation(this,R.anim.fade_in);
myImageView.startAnimation(fade);

$ b $ **
* @param查看当前视图
*打开用户登录屏幕
*如果用户已经签名,然后打开主要活动
*否则,打开firebase登录
* /
public void openLoginScreen(View view){
auth = FirebaseAuth.getInstance();
mUserDatabase = FirebaseDatabase.getInstance()。getReference(users);
mReportDatabase = FirebaseDatabase.getInstance()。getReference(Reports); ();
if(auth.getCurrentUser()!= null){
Log.d(LOGGED,auth.getCurrentUser()。getEmail());
Intent intent = new Intent(WelcomeActivity.this,MainActivity.class);
setUser();
startActivity(intent);
finish();
} else {
//如果当前用户为空(即,未登录),则打开firebase登录
startActivityForResult(AuthUI.getInstance()
.createSignInIntentBuilder()
.setProviders (AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),$ b $ new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build(),$ b $ new AuthUI.IdpConfig。 Builder(AuthUI.FACEBOOK_PROVIDER).build()))
.build(),
RC_SIGN_IN);


$ b $ **
*从firebase中检索用户详细信息,并在模型中设置本地当前用户
* /
public static void setUser(){
Log.d(LOGGED,SET USER CALLED);
String uniqueId = FirebaseAuth.getInstance()。getCurrentUser()。getUid();
String email = FirebaseAuth.getInstance()。getCurrentUser()。getEmail();
Log.d(EMAIL FROM FIREBASE,email);
String name = mUserDatabase.child(uniqueId).child(name)。orderByValue()。toString();
int zip = mUserDatabase.child(uniqueId).child(zipcode)。orderByValue()。hashCode();
String phone = mUserDatabase.child(uniqueId).child(phone number)。orderByValue()。toString();

DatabaseReference typ = mUserDatabase.child(uniqueId).child(type);
typ.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
Log.d(LOGGING,Entered);
$ type = dataSnapshot.getValue()。toString();

$ b @Override
public void onCancelled(DatabaseError e){
Log.d(错误,e.getMessage());
}
});
//在模型
中本地创建一个用户if(type.equals(user)){
Model.getInstance()。setCurrentUser(new User(uniqueId,email,name,邮编,电话));
} else if(type.equals(worker)){
Model.getInstance()。setCurrentUser(new Worker(uniqueId,email,name,zip,phone));
} else if(type.equals(manager)){
Model.getInstance()。setCurrentUser(new Manager(uniqueId,email,name,zip,phone));
} else if(type.equals(admin)){
Model.getInstance()。setCurrentUser(new Administrator(uniqueId,email,name,zip,phone));


$ b / **
* @param requestCode请求码
* @param resultCode结果码
* @param data确定响应的意图
*
* /
$ b $保护void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode, resultCode,data);
if(requestCode == RC_SIGN_IN){
IdpResponse response = IdpResponse.fromResultIntent(data);

//成功登录
if(resultCode == ResultCodes.OK){
String uniqueId = FirebaseAuth.getInstance()。getCurrentUser()。getUid();
String email = FirebaseAuth.getInstance()。getCurrentUser()。getEmail();
// final String displayName = FirebaseAuth.getInstance()。getCurrentUser()。getDisplayName();
DatabaseReference UserRef = mUserDatabase.child(uniqueId);
UserRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
Object userData = dataSnapshot.getValue();
意图意图;
if(userData == null){
//mUserDatabase.child(uniqueId).child (\"email\" ).setValue(email);
//mUserDatabase.child(uniqueId) .child(name).setValue(displayName);
intent = new Intent(WelcomeActivity.this,CreateProfile.class);
} else {
//mUserDatabase.setValue(uniqueId) ;
//mUserDatabase.child (\"email\").setValue(email);
setUser();
intent = new Intent(WelcomeActivity.this,MainActivity.class);
}
startActivity(intent);
finish();
}

@Override
public void onCancelled(DatabaseError databaseError){
$ b}
});
Log.d(AUTH,auth.getCurrentUser()。getEmail());

Intent intent = new Intent(WelcomeActivity.this,MainActivity.class);
startActivity(intent);
finish();






$是我的gradle文件的样子

  apply plugin:'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion25.0.2
defaultConfig {
applicationIdedu.gatech.cs2340.waterfall
minSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName1.0
testInstrumentationRunnerandroid.support.test.runner.AndroidJUnitRunner
multiDexEnabled true;

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}


$ b编译fileTree(dir:'libs',include:['* .jar'])
androidTestCompile( 'com.android.support.test.espresso:espresso-core:2.2.2',{
exclude group:'com.android.support',module:'support-annotations'
})

compile'c​​om.android.support:appcompat-v7:25.1.1'
compile'c​​om.android.support:design:25.1.1'
compile'c​​om.firebaseui :firebase-ui-auth:1.1.1'
compile'c​​om.android.support:multidex:1.0.1'
compile'c​​om.google.firebase:firebase-auth:10.0.1'
编译'com.google.firebase:firebase-database:10.0.1'
编译'com.google.firebase:firebase-core:10.0.1'
compile'c​​om.android.support 。约束:布局:1.0.1'
testCompile'junit:junit:4.12'
}
apply插件:'com.google.gms.google-services'


解决方案

我有同样的错误!你在这里忽略的是onDataChange()的异步特性,没有记录的原因是在你完成检索数据之前有一个错误。为了解决这个问题,你需要在你的onDataChange()中实现一个回调机制,如下所示:
$ b $ pre $ public interface OnGetDataListener {
//创建新的回调接口
void onSuccess(DataSnapshot dataSnapshot);
void onStart();
void onFailure();





然后,使用readData方法从snapShot读取数据,所有这些当读取数据时调用onSuccess方法。

pre $ public $ readData(DatabaseReference ref,final OnGetDataListener listener){
listener.onStart();
ref.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
listener.onSuccess(dataSnapshot);
}

@Override
public void onCancelled(DatabaseError databaseError){
listener.onFailure();
}
});






最后,在你的setUser方法中,实现你所需要的一切在你的onSuccess方法中做。我想这样的东西应该工作。
$ b $ pre $ public void setUser(DatabaseReference ref){
readData(ref,new OnGetDataListener (){
@Override
public void onSuccess(DataSnapshot dataSnapshot){
//无论您需要如何处理数据
}
@Override
public void onStart(){
//无论您需要做什么onStart
Log.d(ONSTART,Started);
}

@Override
public void onFailure(){


});
}


In the setUser method, I am trying to read data from my database which I will use to create an instance of User, Worker, Manager, or Administrator.I also know that the following three lines of code are incorrect, but they don't raise any errors, so they can be ignored for now.

    String name = mUserDatabase.child(uniqueId).child("name").orderByValue().toString();
    int zip =  mUserDatabase.child(uniqueId).child("zipcode").orderByValue().hashCode();
    String phone = mUserDatabase.child(uniqueId).child("phone number").orderByValue().toString();

What baffles me is that onDataChange is entered in onActivityResult but not in setUser. I cannot understand why onDataChange is not entered in setUser. The firebase reference is correct, and I navigated to the url logged with typ.toString() and it went to the correct entry. I also have set permissions to public, if that is relevant. I am also connected to the Internet. I also looked at other answers to similar questions which had answers that are not related to the issue. What could be happening that would cause I would appreciate any help!

package edu.gatech.cs2340.waterfall.controller;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.ResultCodes;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.Arrays;

import edu.gatech.cs2340.waterfall.R;
import edu.gatech.cs2340.waterfall.model.Administrator;
import edu.gatech.cs2340.waterfall.model.Manager;
import edu.gatech.cs2340.waterfall.model.Model;
import edu.gatech.cs2340.waterfall.model.User;
import edu.gatech.cs2340.waterfall.model.Worker;

import static android.R.attr.data;
import static android.R.attr.dateTextAppearance;
import static android.R.attr.type;

public class WelcomeActivity extends AppCompatActivity {

    private static FirebaseAuth auth;
    private static final int RC_SIGN_IN = 0;
    private static DatabaseReference mUserDatabase;
    private static DatabaseReference mReportDatabase;
    private static String type;
    public static FirebaseAuth getAuth() {
        return auth;
    }

    public static DatabaseReference getmUserDatabase() {
        return mUserDatabase;
    }

    public static DatabaseReference getmReportDatabase() {
        return mReportDatabase;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ImageView myImageView = (ImageView)findViewById(R.id.logo);
        Animation fade = AnimationUtils.loadAnimation(this, R.anim.fade_in);
        myImageView.startAnimation(fade);
    }

    /**
     * @param view the current view
     * Open a login screen for the user
     *  If the user is already signed, then open the main activity
     *  Otherwise, open the firebase login
     */
    public void openLoginScreen(View view) {
        auth = FirebaseAuth.getInstance();
        mUserDatabase = FirebaseDatabase.getInstance().getReference("users");
        mReportDatabase = FirebaseDatabase.getInstance().getReference("Reports");
        if (auth.getCurrentUser() != null) {
            Log.d("LOGGED", auth.getCurrentUser().getEmail());
            Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
            setUser();
            startActivity(intent);
            finish();
        } else {
            //open firebase login if current user is null i.e. not signed in
            startActivityForResult(AuthUI.getInstance()
                            .createSignInIntentBuilder()
                            .setProviders(Arrays.asList(new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
                                    new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build(),
                                    new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build()))
                            .build(),
                    RC_SIGN_IN);
        }
    }

    /**
     * Retrieve the user details from firebase and set the local current user in the model
     */
    public static void setUser() {
        Log.d("LOGGED", "SET USER CALLED");
        String uniqueId = FirebaseAuth.getInstance().getCurrentUser().getUid();
        String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
        Log.d("EMAIL FROM FIREBASE", email);
        String name = mUserDatabase.child(uniqueId).child("name").orderByValue().toString();
        int zip =  mUserDatabase.child(uniqueId).child("zipcode").orderByValue().hashCode();
        String phone = mUserDatabase.child(uniqueId).child("phone number").orderByValue().toString();

        DatabaseReference typ = mUserDatabase.child(uniqueId).child("type");
        typ.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.d("LOGGING", "Entered");
                type = dataSnapshot.getValue().toString();

            }
            @Override
            public void onCancelled(DatabaseError e) {
                Log.d("Error", e.getMessage());
            }
        });
        //make a user locally in the model
        if (type.equals("user")) {
            Model.getInstance().setCurrentUser(new User(uniqueId, email, name, zip, phone));
        } else if (type.equals("worker")) {
            Model.getInstance().setCurrentUser(new Worker(uniqueId, email, name, zip, phone));
        } else if (type.equals("manager")) {
            Model.getInstance().setCurrentUser(new Manager(uniqueId, email, name, zip, phone));
        } else if (type.equals("admin")) {
            Model.getInstance().setCurrentUser(new Administrator(uniqueId, email, name, zip, phone));
        }
    }

    /**
     * @param requestCode The request code
     * @param resultCode The result code
     * @param data The intent which determines the response
     *
     */

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            IdpResponse response = IdpResponse.fromResultIntent(data);

            // Successfully signed in
            if (resultCode == ResultCodes.OK) {
                String uniqueId = FirebaseAuth.getInstance().getCurrentUser().getUid();
                String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
                //final String displayName = FirebaseAuth.getInstance().getCurrentUser().getDisplayName();
                DatabaseReference UserRef = mUserDatabase.child(uniqueId);
                UserRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        Object userData = dataSnapshot.getValue();
                        Intent intent;
                        if (userData == null) {
                            //mUserDatabase.child(uniqueId).child("email").setValue(email);
                            //mUserDatabase.child(uniqueId).child("name").setValue(displayName);
                            intent = new Intent(WelcomeActivity.this, CreateProfile.class);
                        } else {
                            //mUserDatabase.setValue(uniqueId);
                            //mUserDatabase.child("email").setValue(email);
                            setUser();
                            intent = new Intent(WelcomeActivity.this, MainActivity.class);
                        }
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
                Log.d("AUTH", auth.getCurrentUser().getEmail());

                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }
}

This is what my gradle file looks like

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "edu.gatech.cs2340.waterfall"
        minSdkVersion 23
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true;
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:appcompat-v7:25.1.1'
    compile 'com.android.support:design:25.1.1'
    compile 'com.firebaseui:firebase-ui-auth:1.1.1'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.google.firebase:firebase-auth:10.0.1'
    compile 'com.google.firebase:firebase-database:10.0.1'
    compile 'com.google.firebase:firebase-core:10.0.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
    testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'

解决方案

I had the same error! What you are neglecting here is the asynchronous nature of onDataChange() and the reason nothing is logged is that there is an error before you it finishes retrieving the data. To fix this you need to implement a callback mechanism inside your onDataChange(), something like this:

public interface OnGetDataListener {
    //make new interface for call back
    void onSuccess(DataSnapshot dataSnapshot);
    void onStart();
    void onFailure();
}

Then, make a readData method to read the data from the snapShot and all this does is call the onSuccess method when data is read.

public void readData(DatabaseReference ref, final OnGetDataListener listener) {
    listener.onStart();
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            listener.onSuccess(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            listener.onFailure();
        }
    });

}

Finally, in your setUser method, implement everything you needed to do in your onSuccess method. I think something like this should work.

public void setUser(DatabaseReference ref) {
        readData(ref, new OnGetDataListener() {
            @Override
            public void onSuccess(DataSnapshot dataSnapshot) {
                //whatever you need to do with the data
            }
            @Override
            public void onStart() {
                //whatever you need to do onStart
                Log.d("ONSTART", "Started");
            }

            @Override
            public void onFailure() {

            }
        });
    }

这篇关于未输入Firebase onDataChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆