Swift 如果日期在过去,则API排除数据

Swift 如果日期在过去,则API排除数据,swift,Swift,如果嵌套列表“calls”包含过去的属性,我将尝试从API响应中排除数据 包括以下数据作为回应: [ { "addressLineOne":"Test", "addressLineTwo":"Test2", "calls":{ "dateTime":1597932000, // a date in the future

如果嵌套列表“calls”包含过去的属性,我将尝试从API响应中排除数据

包括以下数据作为回应:

[
   {
      "addressLineOne":"Test",
      "addressLineTwo":"Test2",
      "calls":{
         "dateTime":1597932000, // a date in the future
      },

]
排除此数据:

[
   {
      "addressLineOne":"Test",
      "addressLineTwo":"Test2",
      "calls":{
         "dateTime":1596193200 // a date in the past
      },

]
我正在使用JSON解码器进行api调用:

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

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

        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.localizedDescription)
                return
            }

            guard let data = data else {return}

            do {
                let clients = try JSONDecoder().decode([Client].self, from: data)
                completion(clients)


            } catch let error {
                print("Failed to create JSON with error: ", error.localizedDescription)
            }
        }.resume()
    }
}

任何方向都将非常感谢

通过添加过滤器并使用内置的
日历
功能检查日期来解决此问题:

class Service {
    static let shared = Service()
    let BASE_URL = "url.com/JsonData"
    let calendar = Calendar.current
    
    func fetchClient(completion: @escaping ([Client]) -> ()) {

        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.localizedDescription)
                return
            }

            guard let data = data else {return}

            do {
                let myDecoder = JSONDecoder()
                myDecoder.dateDecodingStrategy = .secondsSince1970 // formats date
                let clients = try myDecoder.decode([Client].self, from: data)
                completion(clients.filter { self.calendar.isDateInToday($0.calls.dateTime) // filters dates upon completion
                }) 


            } catch let error {
                print("Failed to create JSON with error: ", error.localizedDescription)
            }
        }.resume()
    }
}
在我的解决方案中,API调用在过滤之前完成,这不太理想,因为这意味着所有数据都是在过滤之前下载的。理想情况下,我希望在下载之前对数据进行过滤。任何人都欢迎为我指出实现这一目标的正确方向


此外,此解决方案仅检查日期是否为今天,而不检查日期是否在未来。

通过添加筛选器并使用内置的
日历
功能检查日期来解决此问题:

class Service {
    static let shared = Service()
    let BASE_URL = "url.com/JsonData"
    let calendar = Calendar.current
    
    func fetchClient(completion: @escaping ([Client]) -> ()) {

        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.localizedDescription)
                return
            }

            guard let data = data else {return}

            do {
                let myDecoder = JSONDecoder()
                myDecoder.dateDecodingStrategy = .secondsSince1970 // formats date
                let clients = try myDecoder.decode([Client].self, from: data)
                completion(clients.filter { self.calendar.isDateInToday($0.calls.dateTime) // filters dates upon completion
                }) 


            } catch let error {
                print("Failed to create JSON with error: ", error.localizedDescription)
            }
        }.resume()
    }
}
在我的解决方案中,API调用在过滤之前完成,这不太理想,因为这意味着所有数据都是在过滤之前下载的。理想情况下,我希望在下载之前对数据进行过滤。任何人都欢迎为我指出实现这一目标的正确方向


此外,此解决方案仅检查日期是否为今天,而不检查日期是否在未来。

如果您控制后端,最好在那里进行筛选(即不返回您不需要的数据)。除此之外,一旦解码,就可以从
客户机数组中取出
。您可以使用集合的筛选功能删除日期在过去的客户机。或者,您正在使用的API通过向查询中添加参数来支持过滤。您尝试过什么?不相关但从不打印
错误。本地化描述在
JSONDecoder/Encoder
catch块中。始终仅打印
错误
实例以获取真正的错误消息。如果你控制后端,最好在那里进行过滤(也就是说,不要返回你不需要的数据),请阅读(接受的)答案。除此之外,一旦解码,就可以从
客户机数组中取出
。您可以使用集合的筛选功能删除日期在过去的客户机。或者,您正在使用的API通过向查询中添加参数来支持过滤。您尝试过什么?不相关但从不打印
错误。本地化描述在
JSONDecoder/Encoder
catch块中。始终仅打印
错误
实例以获取真正的错误消息。请同时阅读您的答案(已接受)