在Swift中使用AWSTask对象的正确方法是什么?

时间:2017-06-22 00:02:18

标签: ios swift amazon-web-services grand-central-dispatch amazon-sqs

您好,并提前感谢您的时间。

在我的代码中,我向AWSSQS发出了各种请求,这些请求都返回AWSTask。我发现使用这些AWSTask对象非常困难,同时还试图将所有特定于AWS的逻辑保存在一个类中,这样我就可以在需要时轻松切换到不同的云服务。

理想情况下,我想要做的是以串行方式异步执行一系列AWS任务。通常我只是将任务添加到自定义串行调度队列,但由于AWSTask对象本身就是异步任务,我无法做到这一点。

这是一个简单的例子,说明了我遇到的问题。它没有任何现实世界的目的,但它很好地说明了问题。下面,我有代码来创建SQS队列,向SQS队列发送消息,从SQS队列接收消息,以及删除SQS队列。让我们说我想以串行,异步的方式做这四件事。换句话说,我想在尝试下一个任务之前确保上一个任务成功。

的ViewController

DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async {
        awsClass.runTest()
        DispatchQueue.main.async {
            print("Test Finished")
        }
    }

AwsClass

public func createQueue(){
    guard let createQueueRequest = AWSSQSCreateQueueRequest() else{fatalError()}

    createQueueRequest.queueName = "TestQueue"

    sqs.createQueue(createQueueRequest).continueWith(block: {(task) -> AnyObject? in
        if task.error != nil {
            print(task.error!)
        }
        else if task.result != nil {
            self.queueUrl = task.result!.queueUrl!
            print("created queue at: \(self.queueUrl!)")
        }
        return nil
    })
}

public func deleteQueue(){
    if queueUrl != nil {
        guard let deleteQueueRequest = AWSSQSDeleteQueueRequest() else{fatalError()}

        deleteQueueRequest.queueUrl = queueUrl

        sqs.deleteQueue(deleteQueueRequest).continueWith(block: {(task) -> AnyObject? in
            if task.error != nil {
                print(task.error!)
            }
            else if task.result != nil {
                print("queue sucessfully deleted from \(self.queueUrl!)")
                self.queueUrl = nil
            }
            return nil
        })
    }
    else{
        print("Queue has already been deleted")
    }
}

public func sendMessage(messageData: String, toConnectId: String) {
    guard let sendMessageRequest = AWSSQSSendMessageRequest() else{fatalError()}
    sendMessageRequest.queueUrl = toConnectId
    sendMessageRequest.delaySeconds = 0
    sendMessageRequest.messageBody = messageData
    sqs.sendMessage(sendMessageRequest).continueWith(block: {(task) -> AnyObject? in
        if task.error != nil {
            print(task.error!)
        }
        else if task.result != nil {
            print("successfully sent message to \(toConnectId)")
        }
        return nil
    })
}

public func receiveMessage(){
    guard let receiveMessageRequest = AWSSQSReceiveMessageRequest() else{fatalError()}
    receiveMessageRequest.queueUrl = self.queueUrl
    receiveMessageRequest.maxNumberOfMessages = 1

    sqs.receiveMessage(receiveMessageRequest).continueWith(block: {(task) -> AnyObject? in
        if task.error != nil {
            print(task.error!)
        }
        else if task.result != nil {
            let message = (task.result?.messages?.first)!
            print("successfully received message with body:  \(message.body ?? "failed")")
        }
        return nil
    })
}

public func runTest(){
    let mySerialQueue = DispatchQueue(label: "mySerialQueue")
    mySerialQueue.sync {
        self.createQueue()
    }
    mySerialQueue.sync {
        self.sendMessage(messageData: "test", toConnectId: "https://someUrl")
    }
    mySerialQueue.sync {
        self.receiveMessage()
    }
    mySerialQueue.sync {
        self.deleteQueue()
    }
}

由于AWSTasks与完成函数异步,因此代码会快速进行所有四次调用,然后在完成这些任务时调用完成函数。相反,我希望在下一个任务开始之前完成第一个任务的完成功能。

2 个答案:

答案 0 :(得分:2)

AWSTask对象意味着"链接"一起。 可以在此处找到文档:http://docs.aws.amazon.com/mobile/sdkforios/developerguide/awstask.html

这里有一个小例子:

sqs.createQueue(/* parameters */).continueWithSuccess(block: {(task) -> Void in
    // Success
    return sqs.sendMessage(/* parameters */)
}).continueWithSuccess(block: {(task) -> Void in
    // Success
    return sqs.receiveMessage(/* parameters */)
}).continueWithSuccess(block: {(task) -> Void in
    // Success
    return sqs.deleteQueue(/* parameters */)
})

答案 1 :(得分:1)

好的,所以我找到了解决问题的方法。它完全按照预期工作,但它在这个讨厌的完成函数链中这样做。如果有人知道更优雅的解决方案,我全都耳朵!

的ViewController

print("Starting Test")
    DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async {
        atomConnector.runTest(completion: {
            print("test finshed")
        })
    }

AwsClass

public func createQueue(completion: @escaping () -> Void){
    guard let createQueueRequest = AWSSQSCreateQueueRequest() else{fatalError()}

    createQueueRequest.queueName = "TestQueue"

    sqs.createQueue(createQueueRequest).continueWith(block: {(task) -> Void in
        if task.error != nil {
            print(task.error!)
        }
        else if task.result != nil {
            self.queueUrl = task.result!.queueUrl!
            print("created queue at: \(self.queueUrl!)")
            completion()
        }
    })
}

public func deleteQueue(completion: @escaping () -> Void){
    if queueUrl != nil {
        guard let deleteQueueRequest = AWSSQSDeleteQueueRequest() else{fatalError()}

        deleteQueueRequest.queueUrl = queueUrl

        sqs.deleteQueue(deleteQueueRequest).continueWith(block: {(task) -> Void in
            if task.error != nil {
                print(task.error!)
            }
            else if task.result != nil {
                print("queue sucessfully deleted from \(self.queueUrl!)")
                self.queueUrl = nil
                completion()
            }
        })
    }
    else{
        print("Queue has already been deleted")
    }
}

public func sendMessage(messageData: String, toConnectId: String, completion: @escaping () -> Void) {
    guard let sendMessageRequest = AWSSQSSendMessageRequest() else{fatalError()}
    sendMessageRequest.queueUrl = toConnectId
    sendMessageRequest.delaySeconds = 0
    sendMessageRequest.messageBody = messageData
    sqs.sendMessage(sendMessageRequest).continueWith(block: {(task) -> Void in
        if task.error != nil {
            print(task.error!)
        }
        else if task.result != nil {
            print("successfully sent message to \(toConnectId)")
            completion()
        }
    })
}

public func receiveMessage(completion: @escaping () -> Void){
    guard let receiveMessageRequest = AWSSQSReceiveMessageRequest() else{fatalError()}
    receiveMessageRequest.queueUrl = self.queueUrl
    receiveMessageRequest.maxNumberOfMessages = 1

    sqs.receiveMessage(receiveMessageRequest).continueWith(block: {(task) -> Void in
        if task.error != nil {
            print(task.error!)
        }
        else if task.result != nil {
            let message = (task.result?.messages?.first)!
            print("successfully received message with body:  \(message.body ?? "failed")")
            self.deleteMessage(receiptHandle: message.receiptHandle, completion: completion)
        }
    })
}

public func deleteMessage(receiptHandle: String?, completion: @escaping () -> Void){
    guard let deleteMessageRequest = AWSSQSDeleteMessageRequest() else{fatalError()}
    deleteMessageRequest.queueUrl = self.queueUrl
    deleteMessageRequest.receiptHandle = receiptHandle

    sqs.deleteMessage(deleteMessageRequest).continueWith(block: {(task) -> Void in
        if task.error != nil {
            print(task.error!)
        }
        else if task.result != nil {
            print("successfully deleted message with receiptHandle:  \(receiptHandle)")
            completion()
        }
    })
}

public func runTest(completion: @escaping () -> Void){

    self.createQueue(completion: {
        self.sendMessage(messageData: "test", toConnectId: "https://someUrl", completion: {
            self.receiveMessage(completion: {
                self.deleteQueue(completion: {
                    completion()
                })
            })
        })
    })

}