在两个 NSOperations 之间传递数据 [英] Passing Data Between Two NSOperations
问题描述
我非常专注地观看了关于 高级 NSOperations 的 WWDC 2015 会议我玩了一下示例代码.
I watched with a great attention the WWDC 2015 sessions about Advanced NSOperations and I played a little bit with the example code.
提供的抽象非常棒,但有些东西我可能不太了解.
The provided abstraction are really great, but there is something I may did not really good understand.
我想在不使用 MOC 的情况下在两个后续操作子类之间传递结果数据.
I would like to pass result data between two consequent Operation subclasses without using a MOC.
想象一下,我有一个 APIQueryOperation
,它有一个 NSData?
属性和使用这个属性的第二个操作 ParseJSONOperation
.我如何向第二个操作提供这个 NSData?
实例?
Imagine I have a APIQueryOperation
which has a NSData?
property and a second operation ParseJSONOperation
consuming this property. How do I provide this NSData?
intance to the second operation ?
我尝试过这样的事情:
queryOperation = APIQueryOperation(request: registerAPICall)
parseOperation = ParseJSONOperation(data: queryOperation.responseData)
parseOperation.addDependency(queryOperation)
但是当我进入 ParseJSONOperation
的 execute
方法时,实例与初始化程序中的不一样.
But when I enter in the execute
method of the ParseJSONOperation
the instance in not the same as the same as in the initialiser.
我做错了什么?
推荐答案
您的问题是您正在使用 nil 值构建 ParseJSONOperation
.由于您有两个操作依赖于这个 NSData
对象,我建议您编写一个包装器对象来存放这些数据.
Your issue is that you are constructing your ParseJSONOperation
with a nil value. Since you have two operations that rely on this NSData
object I would suggest you write a wrapper object to house this data.
为了尝试与 WWDC 谈话保持一致,让我们将此对象称为 APIResultContext:
To try and be aligned with the WWDC talk lets call this object the APIResultContext:
class APIResultContext {
var data: NSData?
}
现在我们可以将这个对象传递给 APIQueryOperation
和 ParseJSONOperation
,这样我们就有了一个可以存储从 API 传输的数据的有效对象.
now we can pass this object into both the APIQueryOperation
and the ParseJSONOperation
so that we have a valid object that can store the data transferred from the API.
这将使查询的构造函数:
This would make the constructors for the query:
let context = APIResultContext()
APIQueryOperation(request: registerAPICall, context: context)
ParseJSONOperation(context: context)
在您的 ParseJSONOperation
中,假设查询在设置数据后完成,您应该能够访问数据.
Inside your ParseJSONOperation
you should be able to access the data assuming the query completes after it sets the data.
正如@CouchDeveloper 指出的那样,data
严格来说不是线程安全的.对于这个简单的例子,因为这两个操作是相互依赖的,我们可以安全地写入和读取,知道这些访问不会同时发生.但是,为了完善解决方案并使上下文线程安全,我们可以添加一个简单的 NSLock
As @CouchDeveloper pointed out, data
is not strictly speaking thread safe. For this trivial example since the two operations are dependent we can safely write and read knowing that these accesses wont take place at the same time. However, to round the solution up and make the context thread safe we can add a simple NSLock
class APIResultContext {
var data: NSData? {
set {
lock.lock()
_data = newValue
lock.unlock()
}
get {
lock.lock()
var result = _data
lock.unlock()
return result
}
}
private var _data: NSData?
private let lock = NSLock()
}
这篇关于在两个 NSOperations 之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!