Pelican
Pelican 是一个用于日志轮转、事件记录或其他定期后台处理的持久批处理库。
示例
以下是使用 Pelican 设置自定义事件记录的示例,例如将任务批量上传到您的 API。
protocol AppEvents: PelicanGroupable {
var eventName: String { get }
var timeStamp: Date { get }
}
// Batch all app events together into the same group
extension AppEvents {
// Used to group events so they can batched
var group: String {
return "Example App Events"
}
/// Pelican will call `processGroup` on a background thread. You should implement it to do whatever processing is
/// relevant to your application. Below is a sample implementation that sends application events as a batch.
static func processGroup(tasks: [PelicanBatchableTask], didComplete: @escaping ((PelicanProcessResult) -> Void)) {
// Construct JSON to pass to your API.
let postData = Data()
API.shared.postEvents(json: postData) { error in
if error == nil {
didComplete(PelicanProcessResult.done)
} else {
// Retry will call process group again until succesful
didComplete(PelicanProcessResult.retry(delay: 0))
}
}
}
}
// Using compiler generated conformance since PelicanBatchableTask refines Codable
struct LogInEvent: PelicanBatchableTask, AppEvents {
let eventName: String = "Log In"
let timeStamp: Date
let userName: String
init(userName: String) {
self.timeStamp = Date()
self.userName = userName
}
// PelicanBatchableTask conformance, used to read and store task to storage
static let taskType: String = String(describing: LogInEvent.self)
}
尽快注册您的批处理任务并初始化 Pelican,例如在 application(_ application: UIApplication, didFinishLaunchingWithOptions...
var tasks = Pelican.RegisteredTasks()
tasks.register(for: LogInEvent.self)
Pelican.initialize(tasks: tasks)
当用户执行事件时,我们使用 Pelican 记录事件,经过 5 秒事件将被分组,然后调用 processGroup
发送它们。
Pelican.shared.gulp(task: LogInEvent(userName: "Ender Wiggin"))
要求
Pelican 需要 Swift 5 或更高版本。
安装
Pelican 通过 CocoaPods 提供,且需要 CocoaPods 1.7.0 或更高版本。
要安装 Pelican,请在您的 Podfile 中添加以下行
pod 'Pelican'
作者
许可证
Pelican是在MIT许可证下可用的。更多信息请参阅LICENSE文件。