(競合方法ではメソッドが存在する可能性があります)Firebaseからデータを取得する必要があります。ベストプラクティスとは何ですか?いつ、どこでスタック作業の中で見つけた答え:Firebaseがデータを取得した時期を知る方法
my app: 3. observe finished 0
my app: 1. Start ObserveSingle
my app: 2. Observe finished 3
(競合方法ではメソッドが存在する可能性があります)Firebaseからデータを取得する必要があります。ベストプラクティスとは何ですか?いつ、どこでスタック作業の中で見つけた答え:Firebaseがデータを取得した時期を知る方法
my app: 3. observe finished 0
my app: 1. Start ObserveSingle
my app: 2. Observe finished 3
Firebaseデータ以下の終了ステップ2
デバッグが観察以下クロージャ内でのみ有効であることを理解する方法
var ref:FIRDatabaseReference!
ref = FIRDatabase.database().reference()
//filling ShopList by favorite
ref.child("goods").observeSingleEvent(of:.value, with: {(snapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
print("my app: 1. Start ObserveSingle")
for snap in snapshots {
let g = Good()
g.barcode = String(describing: snap.childSnapshot(forPath: "barcode").value!)
g.name = snap.childSnapshot(forPath: "name").value! as! String
g.price = snap.childSnapshot(forPath: "minprice").value! as! Double
g.favorite = snap.childSnapshot(forPath: "favorite").value! as! Bool
if g.favorite {//избранные товары
DataService.dataService.dbShopList.append(g)
}
}
print("my app: 2. Observe finished \(DataService.dataService.dbShopList.count)")
}
})
print("my app: 3. observe finished \(DataService.dataService.dbShopList.count)")
。ですから、
ref.child("goods").observeSingleEvent(of:.value, with: {
//this is the closure where the data is returned and valid
})
を呼び出したときにここで
print("my app: 2.
コードがインターネットよりも速く実行されるため
前にゲッチャ....print("my app: 3.
が呼び出されますよ!
Firebaseがデータを返すのに遅れがあり、の終了後に閉鎖がの後に起こる原因となります。
Firebaseデータで作業する唯一の時間であるので、クロージャの内側に挿入します。準備が整う前にそのデータにアクセスしようとすると、クロージャが呼び出されるまで変数にデータが設定されないため、データがゼロになります。
返されるFirebaseデータに基づいて一連のイベントの設定に慣れるまでに時間がかかります。
myDataSource = []
Query Firebase for data {
populate the data source with Firebase data
reload the tableView
}
don't access the data source here as it won't be ready
我々はあなたが求めるしようとしているどのようなあなたの質問
var ref:FIRDatabaseReference!
ref = FIRDatabase.database().reference()
ref.child("goods").observeSingleEvent(of:.value, with: {(snapshot) in
print("data from the snapshot is ready to be used")
for child in snapshot.children {
var g = Good()
let snap = child as! FIRDataSnapshot
let dict = snap.value! as! [String:String]
g.barcode = dict["barcode"]!
DataService.dataService.dbShopList.append(g)
}
print("All the firebase data was read in, show it in the tableView")
self.tableView.reloadData()
})
print("This will execute before the closure completes so")
print(" don't do anything with the Firebase data here")
からのコードスニペットを使用して練習にそれを置くことができます。
ここにいくつかの擬似コードですか?あなたのプリントには、事態が発生したときに表示されます。 – jonrsharpe