无法在swift中关闭回调?

时间:2017-07-10 08:39:14

标签: ios swift xcode callback closures

我有viewcontroller从我上传媒体到firebase&将API的媒体发送到我的服务器作为API调用。现在上传图像&调用API我创建了seprate类。我创建了一个执行上传工作的函数。

OfflineSync类功能

 func UploadMediaToFireBase(sucess:@escaping ()->(),failure:@escaping (DIError)->()) {


            DIFirebaseImageManager.firebaseInstance.uploadImage(data: data, withName: filepath, mimeType: mimeType, progress: { (progress)
                in


            }) { (callback, firebaseUrl, error) in

                if let url = firebaseUrl {
                    self.uploadplace(place: self.place, done: {
                        sucess()
                        Utility.shared.hideLoader()

                    }, failed: { (result) in
                        failure(result)
                        Utility.shared.hideLoader()
                    })
                }

            }
        }

现在,当我回复网址时,我在

中调用此功能
 func uploadplace(place:place,done:@escaping ()->(),failed:@escaping (DIError)->()) {

            if let placeId = place.id {
                placeManager.shared.editplace(placeId: placeId, place: place, success: { (success) in
                    AlertBar.show(.success, message: AppMessages.place.placeEdited)

                    done()
                }, failure: { (failure) in
                    self.status = .cancelled
                    failed(failure)
                })
            }

    } 

我在firebase&amp ;;上载媒体后url保存在我的服务器上然后我将回调发送到我的控制器,如下面的代码

   OfflineSync.shared.UploadMediaToFireBase(sucess: {
    DILog.print(items: "CallBack to MediaController")
    self.navigationController?.popViewController(animated: true)
    }, failure: { (failure) in

    })
    }

但问题是我从来没有得到任何回调到我的控制器。我得到uplaodplace回调到uplaodmediatofirebase函数。但我从来没有得到回调给我的控制者。请告诉我的问题是什么?< / p>

1 个答案:

答案 0 :(得分:1)

尝试在主线程

上调用闭包方法

例如,

DispatchQueue.main.async{
    sucess()
}

类似,

  • failure(result)

  • done()

  • failed(failure)

对于其他人。

重构代码

此外,您似乎还没有处理if let语句的其他条件。

if let url = firebaseUrl {
                    self.uploadplace(place: self.place, done: {
                        sucess()
                        Utility.shared.hideLoader()

                    }, failed: { (result) in
                        failure(result)
                        Utility.shared.hideLoader()
                    })
                }else{
                        failure(failureResult)
                }

此外,

if let placeId = place.id {
                placeManager.shared.editplace(placeId: placeId, place: place, success: { (success) in
                    AlertBar.show(.success, message: AppMessages.place.placeEdited)

                    done()
                }, failure: { (failure) in
                    self.status = .cancelled
                    failed(failure)
                })
            }else{
                    failed(someFailure)
            }