我正在運行一個 for 回圈函式,以從 iOS 應用程式向 Firestore 中的現有集合創建/添加新檔案。雖然我只創建了大約 48 個檔案,這遠低于基于檔案的規定寫入限制(每秒 500 個檔案) - 
這是正常的事情嗎?還是我的 swift 代碼沒有“優化”?'
根據@Joakim Danielson 的建議,我嘗試向 Firestore 添加/創建較少數量的檔案,但延遲或創建速率保持不變,每個檔案的創建時間約為 0.2 秒。
我對應用程式的用戶體驗感到非常擔心,盡管這樣的創建程序只為新用戶發生過一次,但我預計會為新用戶創建更多的檔案。請在下面查看我的代碼,讓我知道是否還有其他我可以利用的最佳實踐。
下面是 Xcode 控制臺的螢屏截圖,供在創建檔案時參考。

func createAndReadUserSpecificDayProgramDataToFirestore() {
print("codementor: createAndReadUserSpecificDayProgramDataToFirestore started")
let tabbar = tabBarController as! MainTabBarController
guard let userID = Auth.auth().currentUser?.uid else {
print("no current user logged in")
return
}
print("create user specific dayprogram profile using for loop and dayprogram list in tabbar")
if tabbar.dayProgramList.count == 0 {
print("tabbar day program list has no day program profile")
return
}
startActivityIndicator()
for dayprogram in tabbar.dayProgramList {
print("dayprogram forloop executed")
var ref: DocumentReference? = nil
print("codementor: document is adding")
ref = self.db.collection("Day").addDocument(data: [
"author_uid": dayprogram.author_uid ?? "",
"barbellWeight": dayprogram.barbellWeight ?? "",
"benchWeight": dayprogram.benchWeight ?? "",
"createDate": dayprogram.createDate ?? Date(),
"dayId": dayprogram.dayId ?? "",
"dayIntensity": dayprogram.dayIntensity ?? "",
"daySequence": dayprogram.daySequence ?? "",
"deadliftWeight": dayprogram.deadliftWeight ?? "",
"defaultRestTime": dayprogram.defaultRestTime ?? "",
"isPublicFlag": dayprogram.isPublicFlag ?? "",
"lastUpdatedBy": dayprogram.lastUpdatedBy ?? "",
"numberOfExercise": dayprogram.numberOfExercise ?? "",
"overheadWeight": dayprogram.overheadWeight ?? "",
"prcntComplete": dayprogram.prcntComplete ?? "",
"squatWeight": dayprogram.squatWeight ?? "",
"status": dayprogram.status ?? "",
"targetRestEndHR": dayprogram.targetRestEndHR ?? "",
"workoutDate": dayprogram.workoutDate ?? "",
"workoutId": dayprogram.workoutId ?? "",
"userID": userID
], completion: { err in
self.programCount = 1
if self.programCount == tabbar.dayProgramList.count {
print("dayprogram forloop function, which is to create user specific profile completed at \(CFAbsoluteTimeGetCurrent())")
self.createAndOrReadUserSpecificDayProgramData()
self.stopActivityIndicator()
print("forlopp dayprogram all calls finished")
}
if let err = err {
print("Error adding user specific dayprogram profile: \(err)")
} else {
print("timestamp successfully adding: \(CFAbsoluteTimeGetCurrent())")
print("User specific dayprogram profile added with ID: \(ref!.documentID)")
}
print("forlopp dayprogram \(ref!.documentID) finished")
})
print("forlopp dayprogram `dispatchGroup.leave()` was here, but this was the wrong place")
}
print("forlopp dayprogram more exucution if there was any")
}
uj5u.com熱心網友回復:
使用批處理而不是上傳單個檔案
func createAndReadUserSpecificDayProgramDataToFirestore() {
print("codementor: createAndReadUserSpecificDayProgramDataToFirestore started")
let tabbar = tabBarController as! MainTabBarController
guard let userID = Auth.auth().currentUser?.uid else {
print("no current user logged in")
return
}
print("create user specific dayprogram profile using for loop and dayprogram list in tabbar")
if tabbar.dayProgramList.count == 0 {
print("tabbar day program list has no day program profile")
return
}
startActivityIndicator()
let batch = db.batch()
for dayprogram in tabbar.dayProgramList {
print("dayprogram forloop executed")
var ref: DocumentReference = db.collection("myCollection").document()
batch.setData([
"author_uid": dayprogram.author_uid ?? "",
"barbellWeight": dayprogram.barbellWeight ?? "",
// your data
"workoutId": dayprogram.workoutId ?? "",
"userID": userID
], forDocument: ref)
self.programCount = 1
}
// this writes everything to the database all at once.
// if ANY of them fail, an error will be thrown
// and the entire operation fails
batch.commit { err in
if self.programCount == tabbar.dayProgramList.count {
print("dayprogram forloop function, which is to create user specific profile completed at \(CFAbsoluteTimeGetCurrent())")
self.createAndOrReadUserSpecificDayProgramData()
self.stopActivityIndicator()
print("forlopp dayprogram all calls finished")
}
}
print("forlopp dayprogram more exucution if there was any")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/388295.html
標籤:ios 迅速 for循环 谷歌云firestore
上一篇:C 中的while回圈
