如何在Firebase完成用户数据下载后重新加载UITableView数据? [英] How to reload UITableView data after Firebase finishes dowload of user data?

查看:68
本文介绍了如何在Firebase完成用户数据下载后重新加载UITableView数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对firebase有问题,因为它是异步的,我不知道如何正确使用它。我的应用程序有待售产品,我从firebase数据库和firebase存储(图片)获得。问题是我不知道何时或如何重新加载我的UITableView以获取所有数据而不会出现nil错误。而且它更复杂,因为首先我需要从firebase获取我的salePost信息,之后我才能获得也在tableview单元格中的用户信息。

so I have a problem with firebase since it's async and I don't know how to work with that properly. My application has feed of products for sale, which I'm getting from firebase database and firebase storage(pictures). The problem is that I don't know when or how to reload my UITableView to get all the data without getting nil error. And it's even more complicated, because first I need to get my salePost information from firebase and only after that I can obtain user Information which is also in tableview cell.

这是我如何获得我的salePosts

This is how I getting my salePosts

    func getSalePosts() -> [SalePost]{

    var salePosts = [SalePost]()

    FIR_DATABASE_SALE_POSTS.queryLimitedToFirst(NUMBER_OF_LOADED_POSTS).observeEventType(.Value, withBlock: { (snapshot) in

        for snap in snapshot.children{

            let imgString = snap.value["image"] as! String!
            let imgNSURL = NSURL(string: imgString)
            let imgNSDATA = NSData(contentsOfURL: imgNSURL!)

            let downloadedImg = UIImage(data: imgNSDATA!)

            let newSalePost = SalePost(title: snap.value["title"] as! String!,
                userUID: snap.value["authorUID"] as! String!,
                postBody: snap.value["body"] as! String!,
                amountAvailable: snap.value["amountAvailable"] as! String!,
                unit: snap.value["unit"] as! String!,
                price: snap.value["price"] as! String!,
                image: downloadedImg!)

            salePosts.append(newSalePost)
            print("num of posts \(salePosts.count)")
        }

    }) { (error) in
            print(error.localizedDescription)
    }

    return salePosts
}

这是获取UserInfo的func(我将从salePost对象获取userUID)

And this is func which is getting UserInfo (I will get the "userUID" from salePost object)

    func getProducer(userUID:String) -> Producer{


    var newProducer:Producer?

    FIR_DATABASE_USERS.child(userUID).observeEventType(.Value, withBlock: { (snap) in

        print(snap.description)

        let imgString = snap.value!["profilePic"] as! String!
        let imgNSURL = NSURL(string: imgString)
        let imgNSDATA = NSData(contentsOfURL: imgNSURL!)
        let downloadedImg = UIImage(data: imgNSDATA!)


        let someProducer = Producer(
            displayName: snap.value!["displayName"] as! String!,
            address: snap.value!["address"] as! String!,
            mobile: snap.value!["mobile"] as! String!,
            email: snap.value!["email"] as! String!,
            password: "",
            bio: snap.value!["bio"] as! String!,
            profilePic: downloadedImg!,
            openingHour: snap.value!["openingHour"] as! String!,
            closingHour: snap.value!["closingHour"] as! String!)

        newProducer = someProducer
        print(newProducer?.description)

        }) { (error) in
            print(error.localizedDescription)
    }

    return newProducer!
}

正如我所说,我不知道何时,何地以及如何调用这些函数(在我的Feed控制器中)使其同步并将所有数据传递给tableview Cells。我将感激你的每一个帮助。我试过谷歌甚至在这里找到它,但没有任何帮助我。

So as I said, I don't know when, where and how to call these functions (in my feed controller) to make it "synchronous" and to get all that data to the tableview Cells right. I will appreciate every help. I tried google it and even find it here, but nothing helped me.

推荐答案

一旦你创建了所有的 salePosts 你应该调用 tableView.reloadData()。因此,在您循环创建将 newSalePost 附加到 salePosts 的位置之后。因为这是一个异步调用,在你的函数结束时返回你的salePosts是没用的。

Once you created all your salePosts you should call tableView.reloadData(). So right after you loop to create where you append your newSalePost to your salePosts. Since this is an asynchronous call returning your salePosts at the end of your function is useless.

我假设你有一个属性 tableView <您的viewController的/ code>并设置委托数据源 tableView 是你的 viewController

I'm assuming that you have an attribute tableView of your viewController and you set the delegate and datasource of your tableView to be your viewController.

不要忘记致电主线程上的tableView.reloadData()

例如: Dispatch.main.async {self。 tableView.reloadData()}

这篇关于如何在Firebase完成用户数据下载后重新加载UITableView数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆