我的应用程序有大约500个用户。在一个屏幕中,我加载了供教师选择的学生用户。学生名单(400)需要20秒才能填充。我想知道是否有更有效的方式来填充数据。要填充的数据是uid,第一个,最后一个,电子邮件。Swift Firebase用户数据的长时间加载时间
这是我的功能来获取学生用户。
var filteredUsers = [User]()
var users = [User]()
fileprivate func fetchFollowingUserIds() {
let ref = Database.database().reference().child("session1AllStudents")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let userIdsDictionary = snapshot.value as? [String: Any] else { return }
print(userIdsDictionary)
userIdsDictionary.forEach({ (key, value) in
HUD.show(.labeledProgress(title: "Finding Students", subtitle: nil))
Database.fetchStudentUsersWithUID(uid: key, completion: { (user) in
self.users.append(user)
print(self.users)
self.users.sort(by: { (u1, u2) -> Bool in
return u1.lastName.compare(u2.lastName) == .orderedAscending
})
self.filteredUsers = self.users
print(self.filteredUsers.count)
self.collectionView?.reloadData()
HUD.hide()
})
})
}) { (err) in
print("Failed to fetch following userids:", err)
}
}
extension Database {
static func fetchStudentUsersWithUID(uid: String, completion: @escaping (User) ->()) {
Database.database().reference().child("studentUsers").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
guard let userDictionary = snapshot.value as? [String: Any] else { return }
let user = User(uid: uid, dictionary: userDictionary)
completion(user)
}) { (err) in
print("Failed to fetch user for posts:", err)
}
}
}
数据库“session1AllStudents”是所有学生的名单仍然让选择和有UID:真正的关键字:值对。
然后我从列表中的UID中获取来自“studentUsers”的学生信息。
--UPDATED--
我在看完评论后使用了以下内容。
func fetchRemainingStudents() {
let ref = Database.database().reference()
ref.child("session1AllStudents").observeSingleEvent(of: .value, with: { (snapshot) in
HUD.show(.labeledProgress(title: "Finding Students", subtitle: nil))
for snap in snapshot.children {
let studentsSnap = snap as! DataSnapshot
let studentsKey = studentsSnap.key
let studentDict = snapshot.value as! [String: Any]
var aStudent = User(uid: studentsKey, dictionary: studentDict)
let userRef = ref.child("studentUsers").child(studentsKey)
userRef.observeSingleEvent(of: .value, with: { snapshot in
let userDict = snapshot.value as! [String:AnyObject]
let firstName = userDict["firstName"] as! String
let lastName = userDict["lastName"] as! String
let email = userDict["email"] as! String
aStudent.firstName = firstName
aStudent.lastName = lastName
aStudent.email = email
self.users.append(aStudent)
self.filteredUsers = self.users
print(self.filteredUsers.count)
self.users.sort(by: { (u1, u2) -> Bool in
return u1.lastName.compare(u2.lastName) == .orderedAscending
})
HUD.hide()
self.collectionView?.reloadData()
})
}
})
}
这有助于加速数据加载。也许下降到1-2秒。
您应该可以在不到一秒钟内加载数千个节点。代码中存在许多问题,可能是问题的一部分。您正在排序,重新分配数组并重新加载tableView,每次通过循环。 – Jay
@Jay你能帮我解决一些可以提高效率的地方吗? –
不知道你是否回顾了我的回答,但为了更好的表现,我稍微更新了它。 – Jay