如何在自定义类中使用Firebase?

时间:2017-07-19 17:39:05

标签: swift class firebase appdelegate

我完成了在我的应用上设置Firebase的步骤,并且测试得很好。但是当我创建一个自定义swift类来创建管理数据库的函数时,它会崩溃并说“未能获得默认的firdatabase实例。必须在使用firdatabase之前调用firapp.configure()”。我在AppDelegate中调用了configure(),但它似乎没有传递给我的自定义类。下面是我得到的错误,我的AppDelegate部分,我称之为FirebaseApp.configure(),以及自定义类。提前谢谢。

DatabaseTestApp [7814:336288] ***由于未捕获的异常'FIRAppNotConfigured'而终止应用程序,原因:'无法获取默认的FIRDatabase实例。在使用FIRDatabase之前必须调用FIRApp.configure()。'

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
        final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
        final CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkbox3);
        final TextView textView = (TextView) findViewById(R.id.text);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = 0;

                if (checkBox1.isChecked()) {
                    ++count;
                }

                if (checkBox2.isChecked()) {
                    ++count;
                }

                if (checkBox3.isChecked()) {
                    ++count;
                }

                textView.setText("How many checked? " + count);
            }
        });
    }
}

我的自定义类:

import UIKit
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    return true
    }
}

1 个答案:

答案 0 :(得分:1)

假设您正在创建某种名为FirebaseManager的类,请执行以下操作:

class FirebaseManager {
    public static let instance = FirebaseManager()
    private init(){
        FirebaseApp.configure()
    }
}