测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | 自定义 |
发布最新发布 | 2017年2月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 David Chiles 维护。
基于 YapDatabase 的持久性串联队列。
为了设置,创建一个处理程序,然后使用名称注册该处理程序。然后在块中返回是否由该处理程序处理给定队列名称。如果一个队列注册到多个处理程序,这可能导致不可预测的行为,因为它们都可能开始操作,但完成时可能会有不同的结果。
let database = YapDatabase(path: path)
let handler = //Some object that conforms to YapTaskQueueHandler
let broker = try! YapTaskQueueBroker.setupWithDatabase(database, name: "handler1", handler: handler) { (queueName) -> Bool in
// return true here if it's a queue that this handler understands and 'handles'
return true
}
// Or instead of checking the queue name in a closure you can just use the broker name as the prefix to a queue.
// So in this case any actionItem that returns a queue starting with "handler1" like "handler1-queue2"
let broker = try! YapTaskQueueBroker.setupWithDatabase(database, name: "handler1", handler: handler)
操作对象应仅包含必要的信息,以便相应的 YapTaskQueueHandler
了解在接到指示时应该做什么。
示例
这是一个简单的消息发送操作的示例
class MessageSendAction:NSObject, NSCoding, YapTaskQueueAction {
let key:String
let collection:String
let messageToSendKey:String
let messagetoSendCollection:String
let queue:String
let date:NSDate
init(key:String, collection:String, messageToSendKey:String,messagetoSendCollection:String, queue:String, date:NSDate) {
self.key = key
self.collection = collection
self.messageToSendKey = messageToSendKey
self.messagetoSendCollection = messagetoSendCollection
self.queue = queue
self.date = date
}
//MARK: YapTaskQueueAction
func yapKey() -> String {
return self.key
}
func yapCollection() -> String {
return self.collection
}
func queueName() -> String {
return self.queue
}
func sort(otherObject: YapTaskQueueAction) -> NSComparisonResult {
guard let otherAction = otherObject as? MessageSendAction else {
return .OrderedSame
}
return self.date.compare(otherAction.date)
}
//MARK: NSCoding
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.key, forKey: "key")
aCoder.encodeObject(self.collection, forKey: "collection")
aCoder.encodeObject(self.messageToSendKey, forKey: "messageToSendKey")
aCoder.encodeObject(self.messagetoSendCollection, forKey: "messagetoSendCollection")
aCoder.encodeObject(self.queue, forKey: "queue")
aCoder.encodeObject(self.date, forKey: "date")
}
required convenience init?(coder aDecoder: NSCoder) {
guard let key = aDecoder.decodeObjectForKey("key") as? String,
let collection = aDecoder.decodeObjectForKey("collection") as? String,
let messageToSendKey = aDecoder.decodeObjectForKey("messageToSendKey") as? String,
let messagetoSendCollection = aDecoder.decodeObjectForKey("messagetoSendCollection") as? String,
let queue = aDecoder.decodeObjectForKey("queue") as? String,
let date = aDecoder.decodeObjectForKey("date") as? NSDate
else {
return nil
}
self.init(key:key,collection: collection, messageToSendKey: messageToSendKey, messagetoSendCollection: messagetoSendCollection, queue: queue, date: date)
}
}
这是一个消息处理程序的粗略轮廓
class MessageHandler:YapTaskQueueHandler {
var databaseConnection:YapDatabaseConnection?
func handleNextItem(action: YapTaskQueueAction, completion: (success: Bool, retryTimeout: NSTimeInterval) -> Void) {
guard let messageAction = action as? MessageSendAction else {
completion(success: false, retryTimeout: -1)
return
}
/**
1. Get the 'real' message out of the database
2. Send the message over the wire
3. get result
*/
let result = true
/**
If the sending was successful then return true and it doesn't matter what you set the `retryTimeout` to
If the sending was not successful then send bask false and when you want to retry
It's also possible to set the retry timeout to -1 if you don't want a timed retry but would rather manually retry when the conditions are more likely to result in a success
completion(success: result, retryTimeout: 5)
*/
completion(success: result, retryTimeout: -1)
}
}
一旦设置好代理和处理器以及数据库,只需保存一个操作,它就会在队列中处理。不建议在处理器内部删除操作。相反,当操作成功完成时,代理会从数据库中删除操作并检查剩余的操作。