- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的 Xcode 版本:6.3.2
Alamofire版本:1.2.2(通过Cocoapods安装)
为了设置 maxConcurrentOperationCount
以限制 NSOperationQueue
中的并发操作数,我将 Alamofire 包装起来NSOperation 中的下载请求只是 like Rob suggested .
NSOperation
的基本子类如下:
class ConcurrentOperation : NSOperation {
override var concurrent: Bool {
return true
}
override var asynchronous: Bool {
return true
}
private var _executing: Bool = false
override var executing: Bool {
get {
return _executing
}
set {
if (_executing != newValue) {
self.willChangeValueForKey("isExecuting")
_executing = newValue
self.didChangeValueForKey("isExecuting")
}
}
}
private var _finished: Bool = false;
override var finished: Bool {
get {
return _finished
}
set {
if (_finished != newValue) {
self.willChangeValueForKey("isFinished")
_finished = newValue
self.didChangeValueForKey("isFinished")
}
}
}
/// Complete the operation
///
/// This will result in the appropriate KVN of isFinished and isExecuting
func completeOperation() {
executing = false
finished = true
}
override func start() {
if (cancelled) {
finished = true
return
}
executing = true
main()
}
}
我的子类包装了这样一个 Alamofire 下载请求:
class DownloadImageOperation : ConcurrentOperation {
let URLString: String
let downloadImageCompletionHandler: (responseObject: AnyObject?, error: NSError?) -> ()
weak var request: Alamofire.Request?
init(URLString: String, downloadImageCompletionHandler: (responseObject: AnyObject?, error: NSError?) -> ()) {
self.URLString = URLString
self.downloadImageCompletionHandler = downloadImageCompletionHandler
super.init()
}
override func main() {
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
request = Alamofire.download(.GET, URLString, destination).response { (request, response, responseObject, error) in
if self.cancelled {
println("Alamofire.download cancelled while downlading. Not proceed.")
} else {
self.downloadImageCompletionHandler(responseObject: responseObject, error: error)
}
self.completeOperation()
}
}
override func cancel() {
request?.cancel()
super.cancel()
}
}
它会覆盖 cancel()
并在 NSOperation
被取消时尝试取消 Alamofire 请求。
我使用了一个 KVO 观察器来观察 NSOperationQueue
的完成。
private var testAlamofireContext = 0
class TestAlamofireObserver: NSObject {
var queue = NSOperationQueue()
init(delegate: ImageDownloadDelegate) {
super.init()
queue.addObserver(self, forKeyPath: "operations", options: .New, context: &testAlamofireContext)
}
deinit {
queue.removeObserver(self, forKeyPath: "operations", context: &testAlamofireContext)
}
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject: AnyObject], context: UnsafeMutablePointer<Void>) {
if context == &testAlamofireContext {
if self.queue.operations.count == 0 {
println("Image Download Complete queue. keyPath: \(keyPath); object: \(object); context: \(context)")
}
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
}
我开始了这样的下载列表:
func downloadImages() {
let imgLinks = [
"https://farm4.staticflickr.com/3925/18769503068_1fc09427ec_k.jpg",
"https://farm1.staticflickr.com/338/18933828356_4f57420df7_k.jpg",
"https://farm4.staticflickr.com/3776/18945113685_ccec89d67a_o.jpg",
"https://farm1.staticflickr.com/366/18333992053_725f21166e_k.jpg",
"https://farm4.staticflickr.com/3777/18962702032_086453ee7a_k.jpg",
"https://farm1.staticflickr.com/373/18930501406_4753ac021a_k.jpg",
"https://farm1.staticflickr.com/283/18772907409_56ffbe573b_k.jpg",
"https://farm1.staticflickr.com/314/18940901785_b0564b1c9b_o.jpg",
"https://farm1.staticflickr.com/502/18949263495_88d75d2d2f_k.jpg",
"https://farm4.staticflickr.com/3912/18938184302_6e0ca9ad31_k.jpg",
"https://farm1.staticflickr.com/356/18957923475_3dc9df7634_k.jpg",
"https://farm1.staticflickr.com/378/18925014986_e87feca9c7_o.jpg",
"https://farm1.staticflickr.com/461/18949863812_ddf700bd03_o.jpg",
"https://farm1.staticflickr.com/303/18920711216_4684ff4295_k.jpg",
"https://farm1.staticflickr.com/558/18935058546_fc10d10855_k.jpg",
"https://farm1.staticflickr.com/384/18955290345_fb93d17828_o.jpg",
"https://farm1.staticflickr.com/366/18333992053_725f21166e_k.jpg",
"https://farm4.staticflickr.com/3777/18962702032_086453ee7a_k.jpg",
"https://farm1.staticflickr.com/373/18930501406_4753ac021a_k.jpg",
"https://farm1.staticflickr.com/283/18772907409_56ffbe573b_k.jpg",
"https://farm1.staticflickr.com/314/18940901785_b0564b1c9b_o.jpg",
"https://farm1.staticflickr.com/502/18949263495_88d75d2d2f_k.jpg",
"https://farm4.staticflickr.com/3912/18938184302_6e0ca9ad31_k.jpg",
"https://farm1.staticflickr.com/356/18957923475_3dc9df7634_k.jpg",
"https://farm1.staticflickr.com/378/18925014986_e87feca9c7_o.jpg",
"https://farm1.staticflickr.com/461/18949863812_ddf700bd03_o.jpg",
"https://farm1.staticflickr.com/303/18920711216_4684ff4295_k.jpg",
"https://farm1.staticflickr.com/558/18935058546_fc10d10855_k.jpg",
"https://farm1.staticflickr.com/366/18333992053_725f21166e_k.jpg",
"https://farm4.staticflickr.com/3777/18962702032_086453ee7a_k.jpg",
"https://farm1.staticflickr.com/373/18930501406_4753ac021a_k.jpg",
"https://farm1.staticflickr.com/283/18772907409_56ffbe573b_k.jpg",
"https://farm1.staticflickr.com/314/18940901785_b0564b1c9b_o.jpg",
"https://farm1.staticflickr.com/502/18949263495_88d75d2d2f_k.jpg",
"https://farm4.staticflickr.com/3912/18938184302_6e0ca9ad31_k.jpg",
"https://farm1.staticflickr.com/356/18957923475_3dc9df7634_k.jpg",
"https://farm1.staticflickr.com/378/18925014986_e87feca9c7_o.jpg",
"https://farm1.staticflickr.com/461/18949863812_ddf700bd03_o.jpg",
"https://farm1.staticflickr.com/303/18920711216_4684ff4295_k.jpg",
"https://farm1.staticflickr.com/558/18935058546_fc10d10855_k.jpg",
"https://farm4.staticflickr.com/3777/18962702032_086453ee7a_k.jpg",
"https://farm1.staticflickr.com/373/18930501406_4753ac021a_k.jpg",
"https://farm1.staticflickr.com/283/18772907409_56ffbe573b_k.jpg",
"https://farm1.staticflickr.com/314/18940901785_b0564b1c9b_o.jpg",
"https://farm1.staticflickr.com/502/18949263495_88d75d2d2f_k.jpg",
"https://farm4.staticflickr.com/3912/18938184302_6e0ca9ad31_k.jpg",
"https://farm1.staticflickr.com/356/18957923475_3dc9df7634_k.jpg",
"https://farm1.staticflickr.com/378/18925014986_e87feca9c7_o.jpg",
"https://farm1.staticflickr.com/461/18949863812_ddf700bd03_o.jpg",
"https://farm1.staticflickr.com/303/18920711216_4684ff4295_k.jpg",
"https://farm1.staticflickr.com/558/18935058546_fc10d10855_k.jpg",
"https://farm1.staticflickr.com/266/18956724112_6e61a743a5_k.jpg"
]
var testAlamofireObserver = TestAlamofireObserver()
testAlamofireObserver!.queue.maxConcurrentOperationCount = 5
for imgLink in imgLinks {
let operation = DownloadImageOperation(URLString: imgLink) {
(responseObject, error) in
if responseObject == nil {
// handle error here
println("failed: \(error)")
} else {
println("\(responseObject?.absoluteString) downloaded.")
}
}
testAlamofireObserver!.queue.addOperation(operation)
}
}
如果队列完成而没有收到任何取消,日志输出应该是:
2015-06-22 17:11:04.206 RSS Wallpaper Switchr[46250:714702] Optional(Optional("https://farm1.staticflickr.com/461/18949863812_ddf700bd03_o.jpg")) downloaded.
...
...
...
2015-06-22 17:11:56.979 RSS Wallpaper Switchr[46250:714702] Optional(Optional("https://farm1.staticflickr.com/461/18949863812_ddf700bd03_o.jpg")) downloaded.
2015-06-22 17:11:56.979 RSS Wallpaper Switchr[46250:714702] Image Download Complete queue. keyPath: operations; object: <NSOperationQueue: 0x6180002354a0>{name = 'NSOperationQueue 0x6180002354a0'}; context: 0x000000010007eb70
如果队列接收到cancelAllOperations()
,日志输出应该是:
2015-06-22 17:16:29.691 RSS Wallpaper Switchr[46467:720630] Optional(Optional("https://farm1.staticflickr.com/366/18333992053_725f21166e_k.jpg")) downloaded.
2015-06-22 17:16:32.632 RSS Wallpaper Switchr[46467:720630] Alamofire.download cancelled while downlading. Not proceed.
...
...
2015-06-22 17:16:32.642 RSS Wallpaper Switchr[46467:720630] Alamofire.download cancelled while downlading. Not proceed.
2015-06-22 17:16:32.643 RSS Wallpaper Switchr[46467:720630] Image Download Complete queue. keyPath: operations; object: <NSOperationQueue: 0x600000024c20>{name = 'NSOperationQueue 0x600000024c20'}; context: 0x000000010007eb70
但是,如果我如上所述将 maxConcurrentOperationCount
更改为非默认值,并且队列接收到 cancelAllOperations()
,则日志变为:
2015-06-22 17:17:56.427 RSS Wallpaper Switchr[46606:722523] Optional(Optional("https://farm4.staticflickr.com/3777/18962702032_086453ee7a_k.jpg")) downloaded.
2015-06-22 17:17:58.675 RSS Wallpaper Switchr[46606:722523] Alamofire.download cancelled while downlading. Not proceed.
...
...
2015-06-22 17:17:58.677 RSS Wallpaper Switchr[46606:722523] Alamofire.download cancelled while downlading. Not proceed.
2015-06-22 17:17:58.678 RSS Wallpaper Switchr[46606:722720] Image Download Complete queue. keyPath: operations; object: <NSOperationQueue: 0x608000424ee0>{name = 'NSOperationQueue 0x608000424ee0'}; context: 0x000000010007eb70
2015-06-22 17:17:58.678 RSS Wallpaper Switchr[46606:722560] Image Download Complete queue. keyPath: operations; object: <NSOperationQueue: 0x608000424ee0>{name = 'NSOperationQueue 0x608000424ee0'}; context: 0x000000010007eb70
2015-06-22 17:17:58.678 RSS Wallpaper Switchr[46606:722574] Image Download Complete queue. keyPath: operations; object: <NSOperationQueue: 0x608000424ee0>{name = 'NSOperationQueue 0x608000424ee0'}; context: 0x000000010007eb70
2015-06-22 17:17:58.678 RSS Wallpaper Switchr[46606:722719] Image Download Complete queue. keyPath: operations; object: <NSOperationQueue: 0x608000424ee0>{name = 'NSOperationQueue 0x608000424ee0'}; context: 0x000000010007eb70
2015-06-22 17:17:58.678 RSS Wallpaper Switchr[46606:722721] Image Download Complete queue. keyPath: operations; object: <NSOperationQueue: 0x608000424ee0>{name = 'NSOperationQueue 0x608000424ee0'}; context: 0x000000010007eb70
2015-06-22 17:17:58.678 RSS Wallpaper Switchr[46606:722572] Image Download Complete queue. keyPath: operations; object: <NSOperationQueue: 0x608000424ee0>{name = 'NSOperationQueue 0x608000424ee0'}; context: 0x000000010007eb70
KVO observeValueForKeyPath
是从多个不同的线程执行的。线程数可以是可变的。这将导致 KVO 的完成函数被执行多次。如果我不更改 maxConcurrentOperationCount
的默认值或不为 Alamofire.Request
request?.cancel()
,则不会发生这种情况。
为什么我关心 KVO 完成函数的不止一次执行?我的目的是启动一个下载队列,当足够的下载完成后,取消剩余的操作,即使是未启动或正在下载,然后为下载做一些事情。完成函数应该只执行一次,有两个因素 (1) 更改 maxConcurrentOperationCount
的默认值 (2) 不要为
可能与它有关。我想知道为什么以及如何纠正这个问题。request?.cancel()
Alamofire.Request
最佳答案
我发现您描述的多个 KVN 行为并不令人惊讶。文档中没有任何内容表明当它取消所有操作时,operations
将产生单个 KVN。事实上,人们可能会安全地推断出您描述的行为应该是预期的(因为它不会抢先杀死所有这些工作线程,而是向每个工作线程发送一条 cancel
消息,并且每个操作都负责在自己的时间对此做出回应;并且我不希望 operations
在操作最终实际完成之前进行更新)。
就我个人而言,我建议完全放弃这种观察者模式。您的代码不应取决于 NSOperationQueue
是否一次删除所有操作。相反,我建议您改为依赖现有的 downloadImageCompletionHandler
闭包,无论请求是否完成都调用它。只需让闭包查看 error
对象,以确定它是否被取消或是否由于其他原因而失败。
如果您的意图是了解所有这些操作何时完成,我不会依赖operations
KVN。相反,我可能会根据所有其他请求创建一个完成操作:
let completionOperation = NSBlockOperation() { // create completion operation
// do whatever you want here
}
for imgLink in imgLinks {
let operation = DownloadImageOperation(URLString: imgLink) { responseObject, error in
if error != nil {
if error!.code == NSURLErrorCancelled && error!.domain == NSURLErrorDomain {
println("everything OK, just canceled")
} else {
println("error=\(error)")
}
}
if responseObject != nil {
println("\(responseObject?.absoluteString) downloaded.")
}
}
completionOperation.addDependency(operation) // add dependency
testAlamofireObserver!.queue.addOperation(operation)
}
NSOperationQueue.mainQueue().addOperation(completionOperation) // schedule completion operation on some other queue (so that when I cancel everything on that other queue, I don't cancel this, too)
关于swift - 取消包裹在 NSOperation 中的 Alamofire 请求会导致多个 KVO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30955993/
有没有办法在 .swift 文件(编译成 .swift 模块)中声明函数,如下所示: 你好.swift func hello_world() { println("hello world")
我正在尝试使用 xmpp_messenger_ios 和 XMPPFramework 在 iOS 上执行 MUC 这是加入房间的代码。 func createOrJoinRoomOnXMPP()
我想在我的应用程序上创建一个 3D Touch 快捷方式,我已经完成了有关快捷方式本身的所有操作,它显示正确,带有文本和图标。 当我运行这个快捷方式时,我的应用程序崩溃了,因为 AppDelegate
我的代码如下: let assetTag = Expression("asset_tag") let query2 = mdm.select(mdm[assetTag],os, mac, lastRe
我的 swift 代码如下所示 Family.arrayTuple:[(String,String)]? = [] Family.arrayTupleStorage:String? Family.ar
这是我的 JSON,当我读取 ord 和 uniq 数据时出现错误 let response2 : [String: Any] = ["Response":["status":"SUCCESS","
我想将 swift 扩展文件移动到 swift 包中。但是,将文件移动到 swift 包后,我遇到了这种错误: "Type 'NSAttributedString' has no member 'ma
使用CocoaPods,我们可以设置以下配置: pod 'SourceModel', :configurations => ['Debug'] 有什么方法可以用 Swift Package Manag
我正在 Xcode 中开发一个 swift 项目。我将其称为主要项目。我大部分都在工作。我在日期选择器、日期范围和日期数学方面遇到了麻烦,因此我开始了另一个名为 StarEndDate 的项目,其中只
这是 ObjectiveC 代码: CCSprite *progress = [CCSprite spriteWithImageNamed:@"progress.png"]; mProgressBar
我正在创建一个命令行工具,在 Xcode 中使用 Swift。我想使用一个类似于 grunt 的配置文件确实如此,但我希望它是像 Swift 包管理器的 package.swift 文件那样的快速代码
我假设这意味着使用系统上安装的任何 swift 运行脚本:#!/usr/bin/swift 如何指定脚本适用的解释器版本? 最佳答案 Cato可用于此: #!/usr/bin/env cato 1.2
代码说完全没问题,没有错误,但是当我去运行模拟器的时候,会出现这样的字样: (Swift.LazyMapCollection (_base:[ ] 我正在尝试创建一个显示报价的报价应用。 这是导入
是否可以在运行 Swift(例如 Perfect、Vapor、Kitura 等)的服务器上使用 RealmSwift 并使用它来存储数据? (我正在考虑尝试将其作为另一种解决方案的替代方案,例如 no
我刚开始学习编程,正在尝试完成 Swift 编程书中的实验。 它要求““编写一个函数,通过比较两个 Rank 值的原始值来比较它们。” enum Rank: Int { case Ace = 1 ca
在您将此问题标记为重复之前,我检查了 this question 它对我不起作用。 如何修复这个错误: error: SWIFT_VERSION '5.0' is unsupported, suppo
从 Xcode 9.3 开始,我在我的模型中使用“Swift.ImplicitlyUnwrappedOptional.some”包裹了我的字符串变量 我不知道这是怎么发生的,但它毁了我的应用程序! 我
这个问题在这里已经有了答案: How to include .swift file from other .swift file in an immediate mode? (2 个答案) 关闭 6
我正在使用 Swift Package Manager 创建一个应用程序,我需要知道构建项目的配置,即 Debug 或 Release。我试图避免使用 .xcodeproj 文件。请有人让我知道这是否
有一个带有函数定义的文件bar.swift: func bar() { println("bar") } 以及一个以立即模式运行的脚本foo.swift: #!/usr/bin/xcrun s
我是一名优秀的程序员,十分优秀!