1

我有一个 Firebase 实时数据库,它是一个数组,但是当某些节点被删除时,数据会从数组转到字典。

例子:

前:

[
   {
      "callDescription":"Call Zero",
      "callDuration":"call Zero",
      "callID":0,
   },
   {
      "callDescription":"ssadsad",
      "callDuration":"5-8 Minutes",
      "callID":1,
   },
   {
      "callDescription":"dfdsfdsfsd",
      "callDuration":"5-8 Minutes",
      "callID":2,
   }
]

然后callID: 0&callID: 1节点被删除,它变成:

{
   "2":{
      "callDescription":"dfdsfdsfsd",
      "callDuration":"5-8 Minutes",
      "callID":2,
   }
}

无论节点是否被删除,如何确保数据保持为数组?

我正在使用 swift,并且我的应用程序需要一个数组,所以每当收到一个 dict 时,我都会收到一个错误。

这是我的解码方式:

class Service {
static let shared = Service()
let BASE_URL = "https://url.com"

func fetchClient(completion: @escaping ([Calls]) -> ()) {

guard let url = URL(string: BASE_URL) else { return }

URLSession.shared.dataTask(with: url) { (data, response, error) in

    // handle error
    if let error = error {
        print("Failed to fetch data with error: ", error)
        return
    }

    guard let data = data else {return}

    do {
        let myDecoder = JSONDecoder()
        let calls = try myDecoder.decode([Calls].self, from: data)
        completion(calls)
    } catch let error {
        print("Failed to create JSON with error: ", error)
    }
}.resume()
}
4

0 回答 0