在此过程中未初始化默认 FirebaseApp ...确保先调用 FirebaseApp.initializeApp(Context)
Default FirebaseApp is not initialized in this process ... Make sure to call FirebaseApp.initializeApp(Context) first
我正在尝试将数据添加到 Firestore。而且我想用匕首来做到这一点。但我一直收到这个错误。你能帮帮我吗......
** 默认 FirebaseApp 未在此过程中初始化 com.example.vvvv。确保先调用 FirebaseApp.initializeApp(Context)。 **
FirestoreREpository
class FirestoreRepository @Inject constructor(private val usersRef:
CollectionReference) {
fun saveSearchResults(userEntities: List<UserEntity>) {
usersRef.add(userEntities).addOnCompleteListener { task ->
when (task.isSuccessful) {
true -> Log.d(ContentValues.TAG, "added:success")
false -> Log.d(ContentValues.TAG, "added:unsuccess")
}
}
}
}
ViewModel
@HiltViewModel
class UserListViewModel @Inject constructor(private val repository: RoomRepository, private
val firestoreRepository: FirestoreRepository) :
ViewModel() {
private val _users = MutableLiveData<List<User>>()
val users: LiveData<List<User>>
get() = _users
var userData: MutableLiveData<List<User>> = MutableLiveData()
fun userSearch(term: String) {
viewModelScope.launch {
loadFromCache(term)
val getPropertiesDeferred =
GithubApi.retrofitService.searchUser(term)
try {
val userEntities: MutableList<UserEntity> = mutableListOf()
val result = getPropertiesDeferred.body()
_users.value = result?.users
result?.users?.forEach {
userEntities.add(
UserEntity(
term = term,
login = it.login,
avatar_url = it.avatar_url
)
)
}
clearSearchResults(term)
updateSearchResults(userEntities, term)
firestoreRepository.saveSearchResults(userEntities) //save
data with firebase
} catch (e: Exception) {
Log.e("userListErr", e.message.toString())
}
}
}
private fun updateSearchResults(userEntities: List<UserEntity>, term:
String) {
viewModelScope.launch(Dispatchers.IO) {
repository.insertSearchResults(userEntities)
loadFromCache(term)
}
}
private fun loadFromCache(term: String) {
viewModelScope.launch(Dispatchers.IO) {
val list = repository.getSearchResults(term)
userData.postValue(list)
}
}
private fun clearSearchResults(term: String) {
viewModelScope.launch(Dispatchers.IO) {
repository.deleteSearchResults(term)
}
}
}
AppModule
@Module
@InstallIn(SingletonComponent::class)
object APPModule {
@Singleton
@Provides
fun getAppDB(context: Application): AppDatabase {
return AppDatabase.getAppDB(context)
}
@Singleton
@Provides
fun getDao(appDB: AppDatabase): AppDao {
return appDB.getDAO()
}
@Provides
fun provideFirebaseFirestore() = FirebaseFirestore.getInstance()
@Provides
fun provideUsersRef(db: FirebaseFirestore) = db.collection("users")
}
我在这个项目中使用了multi-module。 id com.google.gms.google-services
到app目录下解决。
我正在尝试将数据添加到 Firestore。而且我想用匕首来做到这一点。但我一直收到这个错误。你能帮帮我吗......
** 默认 FirebaseApp 未在此过程中初始化 com.example.vvvv。确保先调用 FirebaseApp.initializeApp(Context)。 **
FirestoreREpository
class FirestoreRepository @Inject constructor(private val usersRef:
CollectionReference) {
fun saveSearchResults(userEntities: List<UserEntity>) {
usersRef.add(userEntities).addOnCompleteListener { task ->
when (task.isSuccessful) {
true -> Log.d(ContentValues.TAG, "added:success")
false -> Log.d(ContentValues.TAG, "added:unsuccess")
}
}
}
}
ViewModel
@HiltViewModel
class UserListViewModel @Inject constructor(private val repository: RoomRepository, private
val firestoreRepository: FirestoreRepository) :
ViewModel() {
private val _users = MutableLiveData<List<User>>()
val users: LiveData<List<User>>
get() = _users
var userData: MutableLiveData<List<User>> = MutableLiveData()
fun userSearch(term: String) {
viewModelScope.launch {
loadFromCache(term)
val getPropertiesDeferred =
GithubApi.retrofitService.searchUser(term)
try {
val userEntities: MutableList<UserEntity> = mutableListOf()
val result = getPropertiesDeferred.body()
_users.value = result?.users
result?.users?.forEach {
userEntities.add(
UserEntity(
term = term,
login = it.login,
avatar_url = it.avatar_url
)
)
}
clearSearchResults(term)
updateSearchResults(userEntities, term)
firestoreRepository.saveSearchResults(userEntities) //save
data with firebase
} catch (e: Exception) {
Log.e("userListErr", e.message.toString())
}
}
}
private fun updateSearchResults(userEntities: List<UserEntity>, term:
String) {
viewModelScope.launch(Dispatchers.IO) {
repository.insertSearchResults(userEntities)
loadFromCache(term)
}
}
private fun loadFromCache(term: String) {
viewModelScope.launch(Dispatchers.IO) {
val list = repository.getSearchResults(term)
userData.postValue(list)
}
}
private fun clearSearchResults(term: String) {
viewModelScope.launch(Dispatchers.IO) {
repository.deleteSearchResults(term)
}
}
}
AppModule
@Module
@InstallIn(SingletonComponent::class)
object APPModule {
@Singleton
@Provides
fun getAppDB(context: Application): AppDatabase {
return AppDatabase.getAppDB(context)
}
@Singleton
@Provides
fun getDao(appDB: AppDatabase): AppDao {
return appDB.getDAO()
}
@Provides
fun provideFirebaseFirestore() = FirebaseFirestore.getInstance()
@Provides
fun provideUsersRef(db: FirebaseFirestore) = db.collection("users")
}
我在这个项目中使用了multi-module。 id com.google.gms.google-services
到app目录下解决。