我有一个客户想要根据两个命令的共同努力检索某些数据。我需要两个命令的原因是第二个命令依赖于第一个命令的数据。我的问题是将数据从第一个命令传递到第二个命令的最佳方式是什么?
我正在使用一个协调对象,它是第一个和第二个命令的委托(delegate) - 但这看起来很困惑。这是否可以在所有命令所在的层中解决,还是需要这个协调对象?
这是客户端类的表示:
class ViewController: UIViewController, CoordinatorDelegate {
// ...
@IBAction func didTouchButton(sender: UIButton) {
self.coordinator.fetchSchoolID(firtName: "Jane", lastName: "Jones")
}
// MARK: - CoordinatorDelegate
func fetchSchoolIDSucceeded(ID ID: Int) {
self.updateUIWithSchoolID(ID)
}
func fetchSchoolIDFailed(error error: NSError) {
self.displayError(error)
}
}
协调器对象:
protocol CoordinatorDelegate {
func fetchSchoolIDSucceeded(ID ID: Int)
func fetchSchoolIDFailed(error error: NSError)
}
struct Coordinator: FetchSchoolInfoActionDelegate, FetchStudentInfoActionDelegate {
let actionFactory: ActionFactory
var delegate: CoordinatorDelegate?
func fetchSchoolID(firstName: String, lastName: String) {
let firstAction = self.actionFactory.fetchStudentInfoAction(firstName: firstName, lastName: lastName, delegate: self)
firstAction.execute()
}
// MARK: - FetchStudentInfoActionDelegate
func fetchStudentInfoSucceeded(studentInfo: StudentInfo) {
let secondAction = self.actionFactory.fetchShoolInfoAction(schoolName: studentInfo.schoolName, delegate: self)
secondAction.execute()
}
func fetchStudentInfoFailed(error: NSError) {
self.delegate?.fetchSchoolIDFailed(error: error)
}
// MARK: - FetchSchoolInfoActionDelegate
func fetchSchoolIDSucceeded(schoolInfo: SchoolInfo) {
self.delegate?.fetchSchoolIDSucceeded(ID: schoolInfo.ID)
}
func fetchSchoolIDFailed(error: NSError) {
self.delegate?.fetchSchoolIDFailed(error: error)
}
}
总之,客户端 ( ViewController
) 想要获取 schoolID
给出 firstName
和 lastName
一个学生。为此,需要执行两个命令 - FetchStudentInfoAction
和 FetchSchoolInfoAction
.第二个命令取决于第一个命令的数据 - 在本例中为 FetchSchoolInfoAction
需要学生的schoolName
由 FetchStudentInfoAction
检索.
这看起来很乱。如果我们想象更多的请求被添加到ViewController
, Coordinator
对象会变得越来越复杂。当第二个命令需要第一个命令的数据时,是否有更好的方法来处理一组顺序命令?这可以由命令层的对象而不是 Coordinator
处理吗? ?
最佳答案
组合操作 (fetchSchoolID) 暗示实体/关系图很容易变得更复杂。我建议您添加一个完成处理程序模式来返回异步请求的结果。
例如,如果 execute() 函数提供了完成处理程序,您将能够在 fetchSchoolID 的实现中获得特定的回调。
let firstAction = self.actionFactory.fetchStudentInfoAction(firstName: firstName, lastName: lastName, delegate: self)
firstAction.execute()
{
(studentInfo) in
let secondAction = self.actionFactory.fetchShoolInfoAction( ....
secondAction.execute ...
}
这将需要对您的 Action 工厂进行轻微的设计更改(以存储和调用完成捕获,但这将大大增加组合和链接 Action 的灵 active 。
要在你的 Action 类中实现这个,你可以这样做:
class FetchStudent:YourBaseActionClass
{
var completionHandler:((StudentInfo)->())? = nil
// execute allow specification of a completion handler
// but doesn't require it
override func execute(completion:((StudentInfo)->())? = nil)
{
completionHandler = completion
}
// I assume you have something like this in here that
// builds the StudentInfo before returning it through the delegate
func fetchComplete()
{
//... your code that prepared the student info
completionHandler?(studentInfo)
}
...
关于ios - 如何在顺序命令之间传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34828455/