我與URLSession工作,一個叫Backendless服務工作。 Backendless與Parse非常相似。異步調用不與URLSession
Backendless有一個通訊服務,允許您發送電子郵件。我在我的應用程序中使用這個錯誤報告。我創建了一個名爲sendErrorCodeEmail()的方法,它調用Backendless方法。下面是一個簡單的例子。
func callSendHTMLEmailInDelegate()
{
let errorCodeMessage = "There is an error."
self.sendErrorCodeEmail(errorCodeMessage)
}
func sendErrorCodeEmail(_ errorCode: String)
{
// Asynchronous Version
let subject = "Error Called"
let body = "\(errorCode)"
let recipient = ["[email protected]"]
self.backendless?.messagingService.sendHTMLEmail(subject, body: body, to: recipient, response: { (response : Any?) ->() in
print("The error code email was sent successfully. \(response)")
}, error: { (fault : Fault?) ->() in
print("The server reported a fault in the sendErrorCode email: \(fault)")
})
}
這很好用。
我的問題是,當我使用sendHTMLEmail用URLSession。如果我報告由於錯誤的URL導致的錯誤,我會調用相同的sendErrorCodeEmail()方法。問題是,Backendless的sendHTMLEmail()方法不會執行。我已經驗證了sendErrorCodeEmail()被調用。
因爲在代碼的唯一區別是使用URLSession的,我想知道是否有我缺少一個線程問題或別的東西。 sendHTMLEmail是一種異步方法。還有一個同步版本,如果我在sendErrorCodeEmail()中調用它,它會起作用。下面是使用URLSession的基本代碼。
func startSession()
{
// Start the connection with the URL that was passed in the unit method in the dataHandler.
self.session = URLSession.shared
let dataTask = self.session!.dataTask(with: self.sessionURL!, completionHandler: { (data, response, error) -> Void in
if error == nil
{
if data != nil
{
print("Data was downloaded successfully")
}
}
else if error != nil
{
self.sendErrorCodeEmail("There was an error")
}
})
dataTask.resume()
}
任何幫助將不勝感激。
看代碼,這樣只會達到'self.sendErrorCodeEmail(「有錯誤」)'代碼時誤差不無,還有另一種情況,錯誤不是零,但數據是零,你沒有覆蓋。添加額外的打印語句/斷點並確保代碼已到達。如果它達到了代碼而不工作,那麼告訴我們發生了什麼,你會得到錯誤嗎? – Scriptable
@Scriptable代碼被簡化以便於閱讀。生產代碼涵蓋上述問題。雖然好點。我很欣賞這些意見。我不打算誤導人。 – jonthornham
無後顧之憂,只是想充分了解的情況下,看起來像它應該工作 – Scriptable