【问题标题】:Firebase database set value doesn't change the valueFirebase 数据库设置值不会更改值
【发布时间】:2021-08-02 14:17:55
【问题描述】:

首先我是初学者

我正在尝试将一些数据上传到 firebase 及其对象集合, 所以我将其转换为 JSON 格式并将其作为字符串上传,就像在 SharedPreference 中保存数组一样 (我的意思是将数组转换为字符串的想法)

规则如下:

{
  "rules" :  {
    ".read" : true,
    ".write" :  true
    }
}

这是数据类:

data class Content(
        val books: List<Book>,
        val poems: List<Poem>,
        val proses:List<Prose>

我创建了一个类来使用 firebase 管理所有进程 这里:

class SdDataManager {

    // get database
    private var database: FirebaseDatabase = Firebase.database
    private var content: Content?
    
    companion object {
        private const val TAG = "SdDataManagerTag"
    }

    init {
        content = null
        
        // read data
        val contentReference = database.getReference("content")
        contentReference.addValueEventListener(object : ValueEventListener {

            override fun onDataChange(snapshot: DataSnapshot) {

                val gson = Gson()
                val json: Map<String, Any>? = snapshot.getValue<Map<String, Any>>()


                content = gson.fromJson(json.toString(), Content::class.java)
            }

            override fun onCancelled(error: DatabaseError) {
                Log.d(TAG, "Failed to read data error: " + error.toException())
            }

        })


    }
    
    // this function is only for testing

    fun test() {
        
        // creating content object
        
        // create test bitmap
        val conf = Bitmap.Config.ARGB_8888
        val bitmap = Bitmap.createBitmap(512, 512, conf)
        
        val chapters = listOf(Section("SEC_TITLE", "SEC_BODY"))
        val book = Book("BOOK_TITLE", chapters, bitmap)
        val books = listOf(book)
        val poem = Poem("POEM_TITLE", "POEM_BODY", bitmap)
        val poems = listOf(poem)
        val prose = Prose("PROSE_TITLE", "PROSE_BODY", bitmap)
        val proseList = listOf(prose)

        
        val testContent = Content(books, poems, proseList)
        updateContent(testContent)

    }
    

    fun getContent(): Content? {
        return content
    }

    fun updateContent(newContent: Content) {
        val gson = Gson()
        val json = gson.toJson(newContent)
        
        val contentReference = database.getReference("content")
        contentReference.setValue(json)
    }
}

现在我正在使用 Junit4 测试这个类 这里:

class SdDataManagerTest {

    private lateinit var sdDataManager: SdDataManager

    @Before
    fun initialize() {
        sdDataManager = SdDataManager()
    }

    @Test
    fun updateContent() {
        sdDataManager.test()
    }
}

当我创建 addOnSuccessListener 时它没有调用

【问题讨论】:

    标签: android firebase kotlin firebase-realtime-database


    【解决方案1】:

    如果您在下载google-services.json 后创建了数据库并且您是在非默认/美国地区创建的,您需要在代码中为您的数据指定 URL,如下所示:

    private var database: FirebaseDatabase = Firebase.database("URL of your database")
    

    【讨论】:

    猜你喜欢
    • 2019-04-08
    • 2017-09-04
    • 1970-01-01
    • 2018-11-18
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    相关资源
    最近更新 更多