Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 使用order by的Firestore时间戳日期范围查询生成错误_Swift_Firebase_Google Cloud Firestore - Fatal编程技术网

Swift 使用order by的Firestore时间戳日期范围查询生成错误

Swift 使用order by的Firestore时间戳日期范围查询生成错误,swift,firebase,google-cloud-firestore,Swift,Firebase,Google Cloud Firestore,我编写了一个查询,根据日期范围过滤一些数据,并希望按某些字段(Int)对结果排序。我编写的代码生成“FIRInvalidArgumentException”异常 如果我删除了“orderby”,那么查询将完美执行。我已经为visitDate和views字段创建了索引 索引:visitDate升序视图降序 使用具有日期范围的“订单依据”是否存在任何问题?我需要为此创建一个特殊索引吗 谢谢你的帮助。@Jay谢谢你给我指明了正确的方向。据文件记载, 但是,如果您有一个带有范围比较(=)的筛选器,则您的

我编写了一个查询,根据日期范围过滤一些数据,并希望按某些字段(Int)对结果排序。我编写的代码生成“FIRInvalidArgumentException”异常

如果我删除了“orderby”,那么查询将完美执行。我已经为visitDate和views字段创建了索引

索引:visitDate升序视图降序

使用具有日期范围的“订单依据”是否存在任何问题?我需要为此创建一个特殊索引吗


谢谢你的帮助。

@Jay谢谢你给我指明了正确的方向。据文件记载,

但是,如果您有一个带有范围比较(=)的筛选器,则您的第一次排序必须在同一字段上:


因此,我必须按visitDate而不是视图来订购它

请在您的问题中包含准确、完整的错误消息和堆栈跟踪,因为它通常包含有关问题原因的重要信息。这不是有效的Firestore查询。您必须按用于该范围的相同字段排序。底部示例不能在不同字段上执行范围筛选和第一个orderBy。这里最好的办法是删除ORDERBY,并在加载数据后在代码中进行排序。
let calendar = Calendar.current

let currentDateComponents = DateComponents(
  calendar: calendar,
  year: calendar.component(.year, from: Date()),
  month: calendar.component(.month, from: Date()),
  day: calendar.component(.day, from: Date()),
  hour: 0,
  minute: 0)

guard let currentDate = calendar.date(from: currentDateComponents) else {
  print("error");
  return
}

guard let weekEndDate = calendar.date(byAdding: .day, value: -7, to: currentDate) else {
  print("error");
  return
}

let queryRef = Firestore.firestore().collection("ArticleVisits")
  .whereField("visitDate", isGreaterThanOrEqualTo: weekEndDate)
  .whereField("visitDate", isLessThanOrEqualTo: currentDate)
  .order(by: "views", descending: true)
  .limit(to: limit)