用户的重构获取功能不会将信息加载到集合视图单元格

时间:2019-07-09 07:29:33

标签: ios swift firebase firebase-realtime-database

所以我有一个函数,如果我在ViewController上使用它,则将所需的信息返回到集合视图单元格并填充其单元格。 当我尝试将所有功能移至服务页面时,进行重构时,当我尝试使用它(重构功能)时,数据将被打印但未加载到集合视图中。

请检查我的代码我缺少什么?

//Service class, where I wanna keep my firebase functions.
class Service {
static let shared = Service()
let hub = JGProgressHUD()

func retrieveUserDogs(uid: String, data: Array<Any>, collectionView: UICollectionView, view: UIView) {
    let uid = uid
    hub.show(in: view)
    let DB_BASE = Dogs_Ref.child(uid)
    var newValue = data
    DB_BASE.observe(.childAdded, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            let infoIndex = UserDogs()
            infoIndex.imageUrl = (dictionary["profileImageUrl"] as! String?)!
            infoIndex.userDogUid = (dictionary["dogUid"] as! String?)!
            newValue.append(infoIndex)
            if snapshot.value != nil {
                let url = URLRequest(url: URL(string: infoIndex.imageUrl)!)
                let task = URLSession.shared.dataTask(with: url) {
                    (data, response, error) in
                    if error != nil {
                        print(error!)
                        return
                    }
                }
                task.resume()
                DispatchQueue.main.asyncAfter(deadline: .now() + 2){
                    collectionView.reloadData()
                    self.hub.dismiss(animated: true)
                }
            } else {
                print("No Data")
            }
        }
        print(snapshot)

    }, withCancel: nil)
  }
}

  //cellforRowAt
 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DogCell", for: indexPath) as! DogCell
    cell.backgroundColor = .clear
    cell.dogImageView.layer.opacity = 0.5

    let index = dogsArray[indexPath.row]
    print("The index value is: \(index)")
    print("The imageurl value is: \(index.imageUrl)")

    cell.dogImageView.sd_setImage(with: URL(string: index.imageUrl )) { (image, err, type, Url) in
        print(index.imageUrl)

    }
    return cell
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let cell = dogsCollectionView.cellForItem(at: indexPath) as! DogCell
    let dogUID = cell.dogId
    print("The result is: \(dogUID)")
    cell.dogImageView.layer.opacity = 0.5
    cell.dogImageView.translatesAutoresizingMaskIntoConstraints = false
    cell.dogImageView.clipsToBounds = true

}

请注意,如果我从函数中删除参数,并在视图控制器中直接使用它,一切将正常工作-请参见以下函数:

  func retrieveUserDogs() {
    hud.show(in: self.view, animated: true)
    let uid = Auth.auth().currentUser?.uid
    let DB_BASE = Database.database().reference().child("Dogs").child(uid!)
    DB_BASE.observe(.childAdded, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
        let infoIndex = UserDogs()
            infoIndex.imageUrl = (dictionary["profileImageUrl"] as! String?)!
            infoIndex.userDogUid = (dictionary["dogUid"] as! String?)!
            self.dogsArray.append(infoIndex)
            if snapshot.value != nil {
                let url = URLRequest(url: URL(string: infoIndex.imageUrl)!)
                let task = URLSession.shared.dataTask(with: url) {
                    (data, response, error) in
                    if error != nil {
                        print(error!)
                        return
                    }
                }
                task.resume()
                DispatchQueue.main.asyncAfter(deadline: .now() + 1){
                    self.dogsCollectionView.reloadData()
                    self.hud.dismiss(animated: true)
                    }
                } else {
                    print("No Data")
                }
            }
            print(snapshot)

        }, withCancel: nil)
    }

0 个答案:

没有答案