【问题标题】:Connection difference between protocol-delegate methods and block协议委托方法和块之间的连接差异
【发布时间】:2014-06-15 11:26:16
【问题描述】:

我正在快速学习并找到了这些用于连接的协议委托方法:

func connection(connection: NSURLConnection!, didFailWithError error: NSError!)

还有另外 3 个...所以他们开始为

connection.start()

但也有块结构:

NSURLConnection.sendSynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in

我理解与“sendAsynchronousRequest”的区别,但是这个块方法有什么区别,什么是最佳实践?

【问题讨论】:

    标签: ios objective-c connection swift


    【解决方案1】:

    我的理解都是异步操作。

    委托方法: 如果您想监控连接状态和接收数据,请使用委托方法

    块: 如果你不想监控,只需要在数据完全接收到后执行一些操作,请使用blocks。

    https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-SW1

    希望有帮助!

    【讨论】:

    • 感谢您的回答!但是你能举一个需要监控的过程的例子吗?下载然后更新 UI 不需要监控,如果我是对的?
    • 1 。委托方法可以检查提供的 NSURLResponse 对象并确定数据的预期内容长度、MIME 类型、建议的文件名以及服务器提供的其他元数据
    • 使用 connection:didReceiveData: 方法向用户提供连接进度的指示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多