How to get a Firestore document size?

前端 未结 5 1013
余生分开走
余生分开走 2020-12-11 04:17

From Firestore docs, we get that the maximum size for a Firestore document is:

Maximum size for a document 1 MiB (1,

5条回答
  •  忘掉有多难
    2020-12-11 04:43

    For Swift users,

    If you want to estimate the document size then I use the following. Returns the estimated size of document in Bytes. It's not 100% accurate but gives a solid estimate. Basically just converts each key, value in the data map to a string and returns total bytes of String + 1. You can see the following link for details on how Firebase determines doc size: https://firebase.google.com/docs/firestore/storage-size.

    func getDocumentSize(data: [String : Any]) -> Int{
            
            var size = 0
            
            for (k, v) in  data {
                
                size += k.count + 1
                
                if let map = v as? [String : Any]{
                    size += getDocumentSize(data: map)
                } else if let array = v as? [String]{
                    for a in array {
                        size += a.count + 1
                    }
                } else if let s = v as? String{
                    size += s.count + 1
                }
        
            }
            
            return size
            
        }
    

提交回复
热议问题