测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
版本最新版本 | 2016年11月 |
SwiftSwift版本 | 3.0 |
SPM支持SPM | ✗ |
由Billy Irwin维护。
这是一个用于在iOS应用程序中跟踪、存储和清空事件的开发者友好型框架。
首先定义要跟踪的事件
class UserImpressionEvent : NSObject, Event {
let user: String
init(user: String) {
self.user = user
super.init()
}
// All objects that conform to Event must implement toString()
func toString() -> String {
return "impression:" + self.user
}
// All objects that conform to Event must also conform to NSCoding
func encode(with aCoder: NSCoder) {
aCoder.encode(self.user, forKey: "user")
}
required convenience init?(coder aDecoder: NSCoder) {
guard let user = aDecoder.decodeObject(forKey: "user") as? String else { return nil }
self.init(user: user)
}
}
然后创建一个EventTracker
// Create a file store to temporarily cache events before flushing
let store = FileEventStore()
// Create a simple flusher that will just log events to the console
// Could also create a flusher that uploads events for processing
let flusher = LogFlusher()
// Set the flush policy to flush after 100 events are tracked
let flushPolicy = EventFlushPolicy.EventLimit(limit: 100)
// Create the EventTracker
let configuration = EventTrackerConfiguration(store: store, flusher: flusher, flushPolicy: flushPolicy)
let tracker = EventTracker(configuration: configuration)
然后跟踪
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let user = self.users[indexPath.row]
cell.render(user)
// Track that the user was shown
tracker.trackEvent(event: UserImpressionEvent(user: user))
return cell
}
EventTracker旨在允许开发人员完全控制事件的定义、存储和清空。
@objc
public protocol Event: class, NSCoding {
func toString() -> String
}
所有跟踪的事件都必须遵守Event协议。Event扩展了NSCoding,以确保所有事件都能够序列化/反序列化为存储。
public protocol EventStore {
func storeEvent(event: Event, completion: @escaping EventStoreCompletion)
func allEvents(completion: @escaping EventStoreReturnCompletion)
func deleteEvents(completion: @escaping EventStoreCompletion)
}
遵守EventStore的类用于在从客户端上传之前临时缓存跟踪事件。事件可以按照最适合开发者的方式存储。EventTracker目前附带两个具体的EventStore类,InMemeoryEventStore和FileEventStore,分别用于在内存和文件中存储事件。
public protocol EventFlusher {
func flushEvents(events: [Event], completion: @escaping EventFlushCompletion)
}
遵守EventFlusher的类负责发出跟踪事件。事件清空器可用来记录所有记录的事件,将它们写入文件,或者最可能的是,将所有跟踪的事件上传到远程服务器。
此外,开发者可以通过使用EventFlushPolicy定义何时清空。可能的选项有:
public class EventTracker {
public func trackEvent(event: Event, completion: EventTrackerCompletion?)
public func flushEvents(completion: EventTrackerCompletion?)
public func clearEvents(completion: EventTrackerCompletion?)
}
EventTracker用于驱动事件存储和清空。它完全线程安全并确保所有操作都按顺序执行。
EventTracker需要Swift 3和XCode 10
要将SQLite.swift安装为Xcode子项目:
将EventTracker.xcodeproj文件拖放到您的项目中。([子模块][],克隆,或[下载][]项目。)
在您的目标通用选项卡中,点击链接框架和库下的+按钮。
选择适合您平台的相应EventTracker.framework。
Twitter:@billy_the_kid
如果您想添加功能,请提交一个问题并/或创建一个pull请求。
EventTracker遵循MIT许可证。有关更多信息,请参阅LICENSE文件。