我是新手,正在尝试使用Swift制作约会应用程序。提取新用户时,我将查询限制为十个用户,但是每次我的查询分页时,总会拉取相同的十个用户。
我正在尝试在最后一个拉出用户之后启动查询,但是它不起作用...代码如下:
fileprivate func fetchUsersFromFirestore() {
let minAge = user?.min ?? FILTxR.defaultMin
let maxAge = user?.max ?? FILTxR.defaultMax
let manQ = user?.isMan ?? FILTxR.defaultIsMan
let womanQ = user?.isWoman ?? FILTxR.defaultIsWoman
print("last fetcehed user XXXXXXXXX \(self.lastFetchedUser?.uid ?? "")")
let query = Firestore.firestore().collection("users").whereField("isAge", isGreaterThanOrEqualTo: minAge).whereField("isAge", isLessThanOrEqualTo: maxAge).whereField("isMan", isEqualTo: manQ).whereField("isWoman", isEqualTo: womanQ).order(by: "isAge", descending: true).order(by: "uid", descending: true).start(after: [lastFetchedUser?.uid ?? ""]).limit(to: 10)
self.topCardView = nil
query.getDocuments { (snapshot, err) in
self.hud.dismiss()
if let err = err {
print("Failed to fetch users:", err)
return
}
var previousCardView: CardView?
snapshot?.documents.forEach({ (documentSnapshot) in
let userDictionary = documentSnapshot.data()
let user = User(dictionary: userDictionary)
self.users[user.uid ?? ""] = user
self.lastFetchedUser = user
print(userDictionary)
print("last fetcehed user is \(self.lastFetchedUser?.uid ?? "")")
let isNotCurrentUser = user.uid != Auth.auth().currentUser?.uid
let hasNotSwipedBefore = self.swipes[user.uid!] == nil
if isNotCurrentUser && hasNotSwipedBefore {
let cardView = self.setupCardFromUser(user: user)
previousCardView?.nextCardView = cardView
previousCardView = cardView
if self.topCardView == nil {
self.topCardView = cardView
}
}
})
}
}
var lastFetchedUser: User?
代码可以正常工作,提取数据并进行构建,但是当我分页时,它只会提取相同的数据。
我正在打印lastFetchedUser并使用UID进行显示,但是由于某种原因,当我尝试在之后开始时,它没有传递到下一个查询中?