我在void函数错误中遇到了意外的非void返回值吗?

时间:2018-12-25 02:30:55

标签: swift firebase firebase-realtime-database switch-statement

error[E0277]: the trait bound `actix::prelude::Request<MyActor, Ping>: futures_core::future::Future` is not satisfied
  --> examples/ping.rs:47:20
   |
47 |         let res =  join_all((1..10).into_iter().map(|x| addr.send(Ping(x))));
   |                    ^^^^^^^^ the trait `futures_core::future::Future` is not implemented for `actix::prelude::Request<MyActor, Ping>`
   |
   = note: required because of the requirements on the impl of `futures_core::future::IntoFuture` for `actix::prelude::Request<MyActor, Ping>`
   = note: required by `futures_util::future::join_all`

error[E0277]: the trait bound `actix::prelude::Request<MyActor, Ping>: futures_core::future::Future` is not satisfied
  --> examples/ping.rs:47:20
   |
47 |         let res =  join_all((1..10).into_iter().map(|x| addr.send(Ping(x))));
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `futures_core::future::Future` is not implemented for `actix::prelude::Request<MyActor, Ping>`
   |
   = note: required by `futures_util::future::JoinAll`

error[E0599]: no method named `map` found for type `futures_util::future::JoinAll<actix::prelude::Request<MyActor, Ping>>` in the current scope
  --> examples/ping.rs:55:12
   |
55 |        res.map(|res| {
   |            ^^^
   |
   = note: the method `map` exists but the following trait bounds were not satisfied:
           `futures_util::future::JoinAll<actix::prelude::Request<MyActor, Ping>> : futures_util::FutureExt`
           `&futures_util::future::JoinAll<actix::prelude::Request<MyActor, Ping>> : futures_util::FutureExt`
           `&mut futures_util::future::JoinAll<actix::prelude::Request<MyActor, Ping>> : futures_util::FutureExt`
           `&mut futures_util::future::JoinAll<actix::prelude::Request<MyActor, Ping>> : futures::Future`
           `&mut futures_util::future::JoinAll<actix::prelude::Request<MyActor, Ping>> : std::iter::Iterator`

重新声明var语句

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    switch section {
    case 0:

从数据库收集信息

        one = Database.database().reference()

出现错误的地方

        one.child("users").child("profile").observe(.value, with: {(snapshot) in
            let num = snapshot.childrenCount

2 个答案:

答案 0 :(得分:1)

您正在传递的要观察的函数被键入为返回void的函数:

{(snapshot) in
    let num = snapshot.childrenCount
    return Int(num)
})

您可以从observe的API文档中看到:

  

声明

func observe(_ eventType: DataEventType, with block: @escaping (DataSnapshot) -> Void) -> UInt

该函数的类型为(DataSnapshot) -> Void,这意味着它无法返回值。但是,您返回的是Int(num)

您将无法从collectionView()返回数据库查询的结果,因为数据库观察器是异步的并立即返回。一段时间后,它将为您带来价值,并且您无法保证会花费多长时间。

答案 1 :(得分:1)

道格·史蒂文森(Doug Stevenson)是对的!

相反,您可以做的是从服务器获取数据,解析并将其保存到变量并更新collectionView之后。

var data = [YourModel]()

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return data.count
}

Database.database().reference().child("users").child("profile").observe(.value, with: {(snapshot) in

    let values = snapshot.values as? [String: Any] else { return }

    var tmpArrayOfValues = [YourModel]()

    // here you create YourModel objects
    values.forEach { (item) in
        let newItem = //create your item from values
        tmpArrayOfValues.append(newItem)
    }    

    self.data = tmpArrayOfValues

    // to update UI you have to return to main thread, because firebase func are running on background thread
    DispatchQueue.main.async {
        self.collectionView.reloadData()
    }
}