PagerViewというライブラリを使用しています。FirebaseRealtimeDatabaseから写真の参照URL等を取得して、FireStorageからData型を取得し、PagerViewに表示をしようとしています。
しかし、クエリし終える前に、numberOfItems()
が呼ばれてしまいます。その前のTabViewController
からリッスンを開始してみたのですが、FireStorageからデータを持ってくる時に時間がかかってしまい、datasourceがnilになってしましました。
どの様にするべきか、またこの場合にcallbackを使う場合は、非同期処理として適切なのでしょうか?
ご教示お願いします。
- Before UITabBarController
class BeforeTabViewController: UITabBarController { var ref: DatabaseReference! var downloadTask: StorageDownloadTask! override func viewDidLoad() { super.viewDidLoad() queryUserShrine() queryAllShrineSimple() } func queryUserShrine(){ guard let userName = UserDefaults.standard.string(forKey: "userName") else { performSegue(withIdentifier: "notFindUser", sender: nil) return } ref = Database.database().reference().child("individual").child(userName) ref.observe(.value) { (snapshot) in if snapshot.exists() { guard let dic = snapshot.value as? [String: Any] else {return} guard let foot = dic["foot"] as? [String] else {return} guard let favorite = dic["favorite"] as? [String] else {return} guard let goodShrine = dic["good"] as? [String] else {print("goodShrine"); return} for i in 0..<foot.count { if foot[i] != "shrine" { ShrineInfoContainer.footShrine.append(foot[i]) } } for t in 0..<favorite.count { if favorite[t] != "shrine" { ShrineInfoContainer.likeShrine.append(favorite[t]) } } for j in 0..<goodShrine.count { if goodShrine[j] != "shrine" { ShrineInfoContainer.goodShrine.append(goodShrine[j]) } } } } } func queryAllShrineSimple() { ref = Database.database().reference().child("pager") ref.observe(.value) { (snapshot) in if snapshot.exists() { for shrines in snapshot.children { let childNodeSnap = shrines as! DataSnapshot guard let dic = childNodeSnap.value as? [String: Any] else {print("dic"); return} guard let mainPhoto = dic["mainPhoto"] as? String else {print("mainPhoto"); return} guard let shrine = dic["name"] as? String else {print("name"); return} guard let shortAdress = dic["shortAdress"] as? String else {print("shortAdress"); return} guard let goodCount = dic["good"] as? Int else {print("good"); return} self.queryPhotoFromStorage(url: mainPhoto) { (data) in ShrineInfoContainer.mainPhotoData.append(data) ShrineInfoContainer.mainPhotoString.append(mainPhoto) ShrineInfoContainer.shortAdressArray.append(shortAdress) ShrineInfoContainer.goodArray.append(goodCount) ShrineInfoContainer.allShrine.append(shrine) } } } } } func queryPhotoFromStorage(url: String, completion: (Data) -> Void ) { let storage = FirebaseStorage.Storage.storage() let photoref = storage.reference(forURL: url ) downloadTask = photoref.getData(maxSize: 1 * 1024 * 1024, completion: { (data, error) in if let error = error { print(error as Any) } else { ShrineInfoContainer.mainPhotoData.append(data) print("photo data is appended.") } }) } }
- ViewController with PagerView
class ViewController: UIViewController, FSPagerViewDelegate, FSPagerViewDataSource{ @IBOutlet weak var pagerView: FSPagerView! { didSet { self.pagerView.register(UINib(nibName: "ShrinePagerView", bundle: nil), forCellWithReuseIdentifier: "cell") } } var ref: DatabaseReference! var refHandle1: DatabaseHandle! var tableItemArray = [ShrineInTableContainer]() var downloadTask: StorageDownloadTask! override func viewDidLoad() { super.viewDidLoad() pagerView.delegate = self pagerView.dataSource = self pagerView.automaticSlidingInterval = 3.0 pagerView.transformer = FSPagerViewTransformer(type: .linear) pagerView.layer.cornerRadius = 10 // Do any additional setup after loading the view. } // MARK: -PagerView func numberOfItems(in pagerView: FSPagerView) -> Int { print("ShrineInfoContainer.allShrine.count ==> (ShrineInfoContainer.allShrine.count)") return ShrineInfoContainer.allShrine.count } func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell { let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index) as! ShrinePagerViewCell let imageData = ShrineInfoContainer.mainPhotoData[index] cell.shrinePicture.image = UIImage(data: imageData) cell.shrineName.text = ShrineInfoContainer.allShrine[index] cell.place.text = ShrineInfoContainer.shortAdressArray[index] if ShrineInfoContainer.footShrine.contains(ShrineInfoContainer.allShrine[index]) { cell.foot.image = UIImage(named: "greenFoot") } else { cell.foot.image = UIImage(named: "normalFoot") } if ShrineInfoContainer.likeShrine.contains(ShrineInfoContainer.allShrine[index]) { cell.star.setImage(UIImage(named: "star"), for: .normal) } else { cell.star.setImage(UIImage(named: "normalStar"), for: .normal) } if ShrineInfoContainer.goodShrine.contains(ShrineInfoContainer.allShrine[index]) { cell.like.setImage(UIImage(named: "heart"), for: .normal) } else { cell.like.setImage(UIImage(named: "normalHeart"), for: .normal) } return cell }
有難うございました。
あなたの回答
tips
プレビュー