英文:
Why is my android app not able to write to FIRESTORE
问题
我已创建一个简单的UI应用程序来写入Firestore,但似乎无法触发OnSuccess或OnFailureListeners。我尝试多次重新连接到Firebase,并遵循了一些Stackoverflow线程,但似乎无法解决我的问题,而且因特网连接没有错误,因为我已经多次检查过...使用Connectivity Manager。
以下是您的代码部分:
private fun updateDatabse(num: String, titleexp: String, gorl: String) {
val user = hashMapOf(
"title" to titleexp,
"expense" to num,
"gorl" to gorl
)
val firestore = Firebase.firestore
firestore.collection("users").document(titleexp).set(user)
.addOnSuccessListener {
Toast.makeText(this, "用户已添加", Toast.LENGTH_LONG).show()
}.addOnFailureListener() {
Toast.makeText(this, "失败!!", Toast.LENGTH_LONG).show()
}
}
我的安全规则如下:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// 此规则允许具有您的Firestore数据库引用的任何人查看、编辑和删除Firestore数据库中的所有数据。这对于开始使用很有用,但它配置为在30天后过期,因为它使您的应用程序容易受到攻击。在那时,所有客户端对您的Firestore数据库的请求都将被拒绝。
// 确保在那之前为您的应用程序编写安全规则,否则在更新规则之前,所有客户端对您的Firestore数据库的请求都将被拒绝。
match /{document=**} {
allow read, write;
}
}
}
请注意,这部分内容仅包括代码和规则,没有其他内容。
英文:
I have created a simple UI app to write to Firestore but the OnSuccess or OnFailureListeners don't seem to trigger I have tried reconnecting to Firebase multiple times and also followed some Stackoverflow threads but I don't seem to be able to solve my issue and there is no error with Internet connection as I have already checked that multiple times...using Connectivity Manager.
Code:-
private fun updateDatabse(num: String, titleexp: String, gorl: String) {
val user= hashMapOf(
"title" to titleexp,
"expense" to num,
"gorl" to gorl
)
//I have tried to enable network as well but it does not update anydata to to Firestore
// FirebaseFirestore.getInstance().enableNetwork()
val firestore= Firebase.firestore
firestore.collection("users").document(titleexp).set(user)
.addOnSuccessListener {
Toast.makeText(this,"User added",Toast.LENGTH_LONG).show()
}.addOnFailureListener(){
Toast.makeText(this,"Failed!!",Toast.LENGTH_LONG).show()
}
}
My Security rules are as follows:-
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// This rule allows anyone with your Firestore database reference to view, edit,
// and delete all data in your Firestore database. It is useful for getting
// started, but it is configured to expire after 30 days because it
// leaves your app open to attackers. At that time, all client
// requests to your Firestore database will be denied.
//
// Make sure to write security rules for your app before that time, or else
// all client requests to your Firestore database will be denied until you Update
// your rules
match /{document=**} {
allow read, write;
}
}
}
Edit:-
Logcat till now:-
2023-07-14 21:42:25.431 458-499 FA com...e.budgettrackerusingfirestore D Connected to remote service
2023-07-14 21:42:25.432 458-499 FA com...e.budgettrackerusingfirestore V Processing queued up service tasks: 5
2023-07-14 21:42:28.108 458-458 TextInputLayout com...e.budgettrackerusingfirestore I EditText added is not a TextInputEditText. Please switch to using that class instead.
2023-07-14 21:42:28.121 458-458 TextInputLayout com...e.budgettrackerusingfirestore I EditText added is not a TextInputEditText. Please switch to using that class instead.
2023-07-14 21:42:28.244 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:28.296 458-495 chatty com...e.budgettrackerusingfirestore I uid=10153(com.example.budgettrackerusingfirestore) RenderThread identical 2 lines
2023-07-14 21:42:28.301 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:28.332 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:28.346 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:28.349 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:29.336 458-458 AssistStructure com...e.budgettrackerusingfirestore I Flattened final assist data: 3512 bytes, containing 2 windows, 24 views
2023-07-14 21:42:30.555 458-499 FA com...e.budgettrackerusingfirestore V Inactivity, disconnecting from the service
2023-07-14 21:42:40.286 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:40.294 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:40.311 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:40.330 458-495 chatty com...e.budgettrackerusingfirestore I uid=10153(com.example.budgettrackerusingfirestore) RenderThread identical 2 lines
2023-07-14 21:42:40.344 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:40.360 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:41.220 458-495 chatty com...e.budgettrackerusingfirestore I uid=10153(com.example.budgettrackerusingfirestore) RenderThread identical 2 lines
2023-07-14 21:42:41.284 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:41.285 458-495 OpenGLRenderer com...e.budgettrackerusingfirestore D endAllActiveAnimators on 0xc88bd380 (DropDownListView) with handle 0xc8897060
2023-07-14 21:42:41.297 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:42.192 458-523 DynamiteModule com...e.budgettrackerusingfirestore W Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
2023-07-14 21:42:42.199 458-523 DynamiteModule com...e.budgettrackerusingfirestore I Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
2023-07-14 21:42:42.199 458-523 ProviderInstaller com...e.budgettrackerusingfirestore W Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
2023-07-14 21:42:42.214 458-523 rusingfirestor com...e.budgettrackerusingfirestore I The ClassLoaderContext is a special shared library.
2023-07-14 21:42:42.238 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
2023-07-14 21:42:42.238 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
2023-07-14 21:42:42.238 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
2023-07-14 21:42:42.238 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed)
2023-07-14 21:42:42.241 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
2023-07-14 21:42:42.241 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
2023-07-14 21:42:42.244 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->objectFieldOffset(Ljava/lang/reflect/Field;)J (greylist,core-platform-api, linking, allowed)
2023-07-14 21:42:42.245 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
2023-07-14 21:42:42.246 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed)
2023-07-14 21:42:42.248 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->allocateInstance(Ljava/lang/Class;)Ljava/lang/Object; (greylist, linking, allowed)
2023-07-14 21:42:42.253 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Llibcore/io/Memory;->peekLong(JZ)J (greylist, reflection, allowed)
2023-07-14 21:42:42.253 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Llibcore/io/Memory;->pokeLong(JJZ)V (greylist, reflection, allowed)
2023-07-14 21:42:42.253 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Llibcore/io/Memory;->pokeInt(JIZ)V (greylist, reflection, allowed)
2023-07-14 21:42:42.253 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Llibcore/io/Memory;->peekInt(JZ)I (greylist, reflection, allowed)
2023-07-14 21:42:42.253 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Llibcore/io/Memory;->pokeByte(JB)V (greylist, reflection, allowed)
2023-07-14 21:42:42.253 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Llibcore/io/Memory;->peekByte(J)B (greylist, reflection, allowed)
2023-07-14 21:42:42.253 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Llibcore/io/Memory;->pokeByteArray(J[BII)V (greylist, reflection, allowed)
2023-07-14 21:42:42.253 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Llibcore/io/Memory;->peekByteArray(J[BII)V (greylist, reflection, allowed)
2023-07-14 21:42:42.254 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->arrayBaseOffset(Ljava/lang/Class;)I (greylist,core-platform-api, linking, allowed)
2023-07-14 21:42:42.254 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->arrayIndexScale(Ljava/lang/Class;)I (greylist, linking, allowed)
2023-07-14 21:42:42.255 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
2023-07-14 21:42:42.255 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
2023-07-14 21:42:42.255 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
2023-07-14 21:42:42.255 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, reflection, allowed)
2023-07-14 21:42:42.255 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, reflection, allowed)
2023-07-14 21:42:42.255 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, reflection, allowed)
2023-07-14 21:42:42.255 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, reflection, allowed)
2023-07-14 21:42:42.255 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, reflection, allowed)
2023-07-14 21:42:42.255 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, reflection, allowed)
2023-07-14 21:42:42.256 458-458 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden field Ljava/nio/Buffer;->address:J (greylist, reflection, allowed)
2023-07-14 21:42:42.257 458-523 rusingfirestor com...e.budgettrackerusingfirestore I The ClassLoaderContext is a special shared library.
2023-07-14 21:42:42.286 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:42.288 458-495 OpenGLRenderer com...e.budgettrackerusingfirestore D endAllActiveAnimators on 0xdcd1a400 (RippleDrawable) with handle 0xc8897060
2023-07-14 21:42:42.289 458-523 rusingfirestor com...e.budgettrackerusingfirestore I The ClassLoaderContext is a special shared library.
2023-07-14 21:42:42.294 458-523 rusingfirestor com...e.budgettrackerusingfirestore I The ClassLoaderContext is a special shared library.
2023-07-14 21:42:42.301 458-523 rusingfirestor com...e.budgettrackerusingfirestore I Waiting for a blocking GC ClassLinker
2023-07-14 21:42:42.306 458-495 EGL_emulation com...e.budgettrackerusingfirestore D eglMakeCurrent: 0xebba5c40: ver 2 0 (tinfo 0xdcc8c260)
2023-07-14 21:42:42.309 458-471 rusingfirestor com...e.budgettrackerusingfirestore I Background young concurrent copying GC freed 30041(1197KB) AllocSpace objects, 0(0B) LOS objects, 15% free, 3443KB/4066KB, paused 612us total 159.973ms
2023-07-14 21:42:42.309 458-523 rusingfirestor com...e.budgettrackerusingfirestore I WaitForGcToComplete blocked ClassLinker on HeapTrim for 7.508ms
2023-07-14 21:42:42.310 458-523 ProviderInstaller com...e.budgettrackerusingfirestore W Failed to report request stats: com.google.android.gms.common.security.ProviderInstallerImpl.reportRequestStats [class android.content.Context, long, long]
2023-07-14 21:42:42.311 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
2023-07-14 21:42:42.328 458-523 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden field Ldalvik/system/BaseDexClassLoader;->pathList:Ldalvik/system/DexPathList; (greylist, reflection, allowed)
2023-07-14 21:42:42.329 458-523 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden field Ldalvik/system/DexPathList;->nativeLibraryDirectories:Ljava/util/List; (greylist, reflection, allowed)
2023-07-14 21:42:42.331 458-523 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden field Ldalvik/system/DexPathList;->systemNativeLibraryDirectories:Ljava/util/List; (greylist, reflection, allowed)
2023-07-14 21:42:42.331 458-523 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden field Ldalvik/system/DexPathList;->nativeLibraryPathElements:[Ldalvik/system/DexPathList$NativeLibraryElement; (greylist, reflection, allowed)
2023-07-14 21:42:42.331 458-523 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Ldalvik/system/DexPathList;->makePathElements(Ljava/util/List;)[Ldalvik/system/DexPathList$NativeLibraryElement; (greylist, reflection, allowed)
2023-07-14 21:42:42.333 458-523 NativeCrypto com...e.budgettrackerusingfirestore V Registering com/google/android/gms/org/conscrypt/NativeCrypto's 286 native methods...
2023-07-14 21:42:42.355 458-523 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Ljava/security/spec/ECParameterSpec;->getCurveName()Ljava/lang/String; (greylist, reflection, allowed)
2023-07-14 21:42:42.358 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
2023-07-14 21:42:42.358 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
2023-07-14 21:42:42.392 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
2023-07-14 21:42:42.396 458-523 ProviderInstaller com...e.budgettrackerusingfirestore I Installed default security provider GmsCore_OpenSSL
2023-07-14 21:42:42.404 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
2023-07-14 21:42:42.408 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
2023-07-14 21:42:42.415 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
2023-07-14 21:42:42.447 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
2023-07-14 21:42:42.447 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
2023-07-14 21:42:42.447 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
2023-07-14 21:42:42.447 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
2023-07-14 21:42:42.450 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
2023-07-14 21:42:42.454 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
2023-07-14 21:42:42.456 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
2023-07-14 21:42:42.460 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
2023-07-14 21:42:42.466 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed)
2023-07-14 21:42:42.471 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
2023-07-14 21:42:42.472 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
2023-07-14 21:42:42.491 458-562 rusingfirestor com...e.budgettrackerusingfirestore W Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
答案1
得分: 0
这只是有效的!! 数据已更新到Firestore。 我刚刚从我使用的Pixel模拟器切换到我的实际三星设备,我不知道为什么在那上面运行应用程序突然使它奇迹般地工作了。 即使模拟器连接到互联网也是如此。
编辑:- 使用物理设备而不是模拟器解决了问题,因为模拟器无法向Firestore添加数据。
英文:
This just worked!! The data is updated to Firestore. I just changed from the Pixel Emulator I use to my physical Samsung device Idk why running the app on that suddenly makes it work but it just did miraculously. Even though the emulator was connected to the internet.
Edit:- Using a physical device instead of an emulator solved the issue as the emulator was unable to add data to Firestore
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论