如何使用 GCD 创建单例串行队列?
How do I create a Singleton Serial Queue with GCD?
这就是我正在使用的(在一个名为 Utils.swift 的 class 中)
static let serialQueue = dispatch_queue_create("com.wi-mobile.wmForms", DISPATCH_QUEUE_SERIAL)
但是,似乎我每次调用类似
的时候都会创建一个新队列
dispatch_async(UtilsUI.serialQueue)
我需要它在另一个 class 中,因为我无法在每次进入 ViewController 时实例化一个新队列我需要它 运行.
我需要发送一些网络调用,但我需要在调用第一个之后立即将 UI 发送到 return,但是我需要将它们全部发送到 运行 串行。
编辑:
我正在使用回调,也许我的序列化失败了,这里有一个例子:
func sendFormToMiddleware() {
successSending = false
errorSending = false
let seconds: String = String(NSDate().timeIntervalSince1970)
let tbvotos: TbvotosDB = TbvotosDB(survey_id: idForm, transaction_id: "\(WMConfiguration.getTranid())", lu_date: seconds , id_movil: WMConfiguration.getUser(), status: "1")
dispatch_async(UtilsUI.serialQueue) {
self.conectionHandler.doPostWmFormsService(self.attemptToSendForm, service: ConnectionHandler.Service.TBVOTOS, parameters: tbvotos.asArray()!)
}
}
func attemptToSendForm(isOk: Bool, mData: [AnyObject], isLastEntity: Bool) -> Void {
if isOk {
successSending = true
Queries.unifyTransactionIDForCurrentFormQuestions(idForm, tranId: WMConfiguration.getTranid())
let responses = Queries.getResponses(idForm)
dispatch_async(UtilsUI.serialQueue) {
self.conectionHandler.insertRespuestas(self.callbackEnvioRespuestas, responses: responses)
}
self.removeAllOverlays()
self.returnAfterSendingForm()
self.view.userInteractionEnabled = true
self.navigationController?.navigationBar.userInteractionEnabled = true
} else {
self.removeAllOverlays()
self.view.userInteractionEnabled = true
self.navigationController?.navigationBar.userInteractionEnabled = true
errorSending = true
UtilsUI.showAlert(NSLocalizedString("Error al contactar con el Servidor", comment: "Fallo conexión middleware"), self)
}
}
这里doPostWmFormsService的第一个参数(attemptToSendForm)是回调函数。如果我是对的,我不知道我应该使用什么其他方法。
它需要在单例上来保持相同的队列,如果它是一个简单的 1 队列或者创建一个专用的单例,您可以使用 AppDelegate。
在您的应用委托中:
static let serialQueue = dispatch_queue_create("com.wi-mobile.wmForms", DISPATCH_QUEUE_SERIAL)
然后去搞定
if let appDel = UIApplication.sharedApplication().delegate as? AppDelegate {
dispatch_async(appDel.serialQueue)
}
我不确定如何在 swift 中对其进行编码,但根据我的 objective c 背景,我建议您使用 dispatch_once ,类似于创建一个函数,在其中初始化队列,然后 return 将其发送给调用者:
{
var singleton:serialQueue
dispatch_once_t onceToken
dispatch_once(&onceToken,{ in singleton = ...
return singleton;
}
然后使用单例
这将创建一个对象,而且只会创建一个对象,并且 return 每次您调用该函数时...
这个问题超出了评论的范围,最初是如何在 Swift 中创建单例队列,我必须感谢 Phillip Mills 打印了两次 print(UtilsUI.serialQueue)
returns 相同的内存地址,因此它是同一个对象,它是一个单例。
这就是我正在使用的(在一个名为 Utils.swift 的 class 中)
static let serialQueue = dispatch_queue_create("com.wi-mobile.wmForms", DISPATCH_QUEUE_SERIAL)
但是,似乎我每次调用类似
的时候都会创建一个新队列dispatch_async(UtilsUI.serialQueue)
我需要它在另一个 class 中,因为我无法在每次进入 ViewController 时实例化一个新队列我需要它 运行.
我需要发送一些网络调用,但我需要在调用第一个之后立即将 UI 发送到 return,但是我需要将它们全部发送到 运行 串行。
编辑:
我正在使用回调,也许我的序列化失败了,这里有一个例子:
func sendFormToMiddleware() {
successSending = false
errorSending = false
let seconds: String = String(NSDate().timeIntervalSince1970)
let tbvotos: TbvotosDB = TbvotosDB(survey_id: idForm, transaction_id: "\(WMConfiguration.getTranid())", lu_date: seconds , id_movil: WMConfiguration.getUser(), status: "1")
dispatch_async(UtilsUI.serialQueue) {
self.conectionHandler.doPostWmFormsService(self.attemptToSendForm, service: ConnectionHandler.Service.TBVOTOS, parameters: tbvotos.asArray()!)
}
}
func attemptToSendForm(isOk: Bool, mData: [AnyObject], isLastEntity: Bool) -> Void {
if isOk {
successSending = true
Queries.unifyTransactionIDForCurrentFormQuestions(idForm, tranId: WMConfiguration.getTranid())
let responses = Queries.getResponses(idForm)
dispatch_async(UtilsUI.serialQueue) {
self.conectionHandler.insertRespuestas(self.callbackEnvioRespuestas, responses: responses)
}
self.removeAllOverlays()
self.returnAfterSendingForm()
self.view.userInteractionEnabled = true
self.navigationController?.navigationBar.userInteractionEnabled = true
} else {
self.removeAllOverlays()
self.view.userInteractionEnabled = true
self.navigationController?.navigationBar.userInteractionEnabled = true
errorSending = true
UtilsUI.showAlert(NSLocalizedString("Error al contactar con el Servidor", comment: "Fallo conexión middleware"), self)
}
}
这里doPostWmFormsService的第一个参数(attemptToSendForm)是回调函数。如果我是对的,我不知道我应该使用什么其他方法。
它需要在单例上来保持相同的队列,如果它是一个简单的 1 队列或者创建一个专用的单例,您可以使用 AppDelegate。
在您的应用委托中:
static let serialQueue = dispatch_queue_create("com.wi-mobile.wmForms", DISPATCH_QUEUE_SERIAL)
然后去搞定
if let appDel = UIApplication.sharedApplication().delegate as? AppDelegate {
dispatch_async(appDel.serialQueue)
}
我不确定如何在 swift 中对其进行编码,但根据我的 objective c 背景,我建议您使用 dispatch_once ,类似于创建一个函数,在其中初始化队列,然后 return 将其发送给调用者:
{
var singleton:serialQueue
dispatch_once_t onceToken
dispatch_once(&onceToken,{ in singleton = ...
return singleton;
}
然后使用单例
这将创建一个对象,而且只会创建一个对象,并且 return 每次您调用该函数时...
这个问题超出了评论的范围,最初是如何在 Swift 中创建单例队列,我必须感谢 Phillip Mills 打印了两次 print(UtilsUI.serialQueue)
returns 相同的内存地址,因此它是同一个对象,它是一个单例。