如何在返回void的函数之外获取视频数据?

时间:2019-04-07 17:27:33

标签: ios swift vimeo

我正在尝试创建一个简单的应用程序,每天从Vimeo获取新视频。我可以正常访问Vimeo和视频,但是内置的“请求”功能默认返回void,我想在请求功能之外使用视频信息(URI,名称,.count等)。

请求函数返回一个称为“ RequestToken”的东西,其中包含一个路径和一个URLSessionDataTask。我以为也许这是关键,但是,由于我是编程新手,所以我一直无法有效使用此信息。我还阅读了很多与VimeoClient和Request等相关的Vimeo类文件,似乎在请求完成后它创建了一个字典对象,但我不知道如何访问它。我觉得这只是我所缺少的一些关于函数/闭包/返回的知识,无法完全找到要回答的互联网搜索术语。

    let videoRequest = Request<[VIMVideo]>(path: "/user/videos")

        vimeoClient.request(videoRequest) { result in
            switch result {
            case .success(let response):
                let video: [VIMVideo] = response.model
                print("retrieved videos: \(video.count)")
            case .failure(let error):
                print ("error retrieving video: \(error)")

这是VimeoClient类下的完整方法调用。

public func request<ModelType>(_ request: Request<ModelType>, completionQueue: DispatchQueue = DispatchQueue.main, completion: @escaping ResultCompletion<Response<ModelType>>.T) -> RequestToken
{
    if request.useCache
    {
        self.responseCache.response(forRequest: request) { result in

            switch result
            {
            case .success(let responseDictionary):

                if let responseDictionary = responseDictionary
                {
                    self.handleTaskSuccess(forRequest: request, task: nil, responseObject: responseDictionary, isCachedResponse: true, completionQueue: completionQueue, completion: completion)
                }
                else
                {
                    let error = NSError(domain: type(of: self).ErrorDomain, code: LocalErrorCode.cachedResponseNotFound.rawValue, userInfo: [NSLocalizedDescriptionKey: "Cached response not found"])

                    self.handleError(error, request: request)

                    completionQueue.async {

                        completion(.failure(error: error))
                    }
                }

            case .failure(let error):

                self.handleError(error, request: request)

                completionQueue.async {

                    completion(.failure(error: error))
                }
            }
        }

        return RequestToken(path: request.path, task: nil)
    }
    else
    {
        let success: (URLSessionDataTask, Any?) -> Void = { (task, responseObject) in

            DispatchQueue.global(qos: .userInitiated).async {

                self.handleTaskSuccess(forRequest: request, task: task, responseObject: responseObject, completionQueue: completionQueue, completion: completion)
            }
        }

        let failure: (URLSessionDataTask?, Error) -> Void = { (task, error) in

            DispatchQueue.global(qos: .userInitiated).async {

                self.handleTaskFailure(forRequest: request, task: task, error: error as NSError, completionQueue: completionQueue, completion: completion)
            }
        }

        let path = request.path
        let parameters = request.parameters

        let task: URLSessionDataTask?

        switch request.method
        {
        case .GET:
            task = self.sessionManager?.get(path, parameters: parameters, progress: nil, success: success, failure: failure)
        case .POST:
            task = self.sessionManager?.post(path, parameters: parameters, progress: nil, success: success, failure: failure)
        case .PUT:
            task = self.sessionManager?.put(path, parameters: parameters, success: success, failure: failure)
        case .PATCH:
            task = self.sessionManager?.patch(path, parameters: parameters, success: success, failure: failure)
        case .DELETE:
            task = self.sessionManager?.delete(path, parameters: parameters, success: success, failure: failure)
        }

        guard let requestTask = task else
        {
            let description = "Session manager did not return a task"

            assertionFailure(description)

            let error = NSError(domain: type(of: self).ErrorDomain, code: LocalErrorCode.requestMalformed.rawValue, userInfo: [NSLocalizedDescriptionKey: description])

            self.handleTaskFailure(forRequest: request, task: task, error: error, completionQueue: completionQueue, completion: completion)

            return RequestToken(path: request.path, task: nil)
        }

        return RequestToken(path: request.path, task: requestTask)
    }
}`

在“打印”语句中,我为页面上的视频获得了正确的计数,因此我知道它在该函数中正常运行。该请求没有明确说明它返回void,但是如果我尝试返回[VIMVideo]对象,则调试会告诉我期望的返回值为Void,然后将无法生成。

我需要的所有信息都可以获取,但是只能在函数内部获取。我希望能够在函数外部使用它,但是我唯一知道的方法就是返回不允许我执行的对象。

感谢您的帮助。

在阅读了有关异步的提供的文档之后,回调似乎是此处的解决之道。但是,回头看一下所讨论的类方法时,它已经内置了完成处理程序。在这里有用的人给我的示例中,完成处理程序将使用可以在回调函数中引用的简单变量。看似内置的VimeoNetworking Pod完成处理程序具有对响应类(可能是responseDictionary的一部分)的复杂外观引用,关于如何在回调中引用该完成处理程序的任何想法?还是那不是目的,我应该尝试在提供的那个处理程序之上构建自己的完成处理程序?

非常感谢!

0 个答案:

没有答案