根据文档,自定义消息类型的实现过程是:“实现 IMMessageCategorizing 协议;
子类将自身类型进行注册,一般可在 AppDelegate 的 application(_:didFinishLaunchingWithOptions:) 方法里面调用 try CustomMessage.register()”
我的问题是,如果已经实现了上述步骤,如果我想在自定义消息里设置两个新变量:温度和湿度,然后用温度湿度合成天气,类似于IMLocationMessage,具体代码该如何实现(增加两个变量:温度和湿度,然后用温度湿度合成天气)?(看了demo里IMLocationMessage实现过程,没太看懂,能不能再单独示例一下代码,尤其是self.weather的实现代码)
/// IM Custom Weather Message
open class CustomWeatherMessage: IMCategorizedMessage {
public class override var messageType: MessageType {
return 1
}
public required init() {
super.init()
}
public init(temperature: Double, humidity: Double) {
super.init()
self.weather = Weather(temperature: temperature, humidity: humidity)
}
@available(*, unavailable)
public override init(application: LCApplication = LCApplication.default, data: Data, format: String? = nil) {
super.init(application: application, data: data, format: format)
}
@available(*, unavailable)
public override init(application: LCApplication = LCApplication.default, filePath: String, format: String? = nil) {
super.init(application: application, filePath: filePath, format: format)
}
@available(*, unavailable)
public override init(application: LCApplication = LCApplication.default, url: URL, format: String? = nil) {
super.init(application: application, url: url, format: format)
}
/// The temperature of weather.
public var temperature: Double? {
return self. weather?. temperature
}
/// The humidity of weather.
public var humidity: Double? {
return self. weather?.humidity
}
}
另外还有一个问题,leancloud/swift-sdk-demo: https://github.com/leancloud/swift-sdk-demo 里面isReopen这个参数好像在函数里面一直没有被使用到,直接用的是Configuration.UserOption.isAutoOpenEnabled.boolValue 。Demo里使用的是Configuration.UserOption.clientID.stringValue来对IMClient进行持久化,这个stringValue可以被持久化存储,如果不用String初始化IMClient,而是使用LCUser初始化IMClient,并且也想用isReopen设置每次自动登录,这个LCUser该如何持久化保存?
Demo这个函数只在参数里列出了,但其实没有使用isReopen:
func open(
client: IMClient,
isReopen: Bool,
storedConversations: [IMConversation]? = nil,
storedServiceConversations: [IMServiceConversation]? = nil)
{
let options: IMClient.SessionOpenOptions
if let _ = client.tag {
options = Configuration.UserOption.isAutoOpenEnabled.boolValue ? [] : [.forced]
} else {
options = .default
}
client.open(options: options, completion: { (result) in
Client.specificAssertion
self.activityToggle()
switch result {
case .success:
mainQueueExecuting {
Client.current = client
Client.storedConversations = storedConversations
Client.storedServiceConversations = storedServiceConversations
UIApplication.shared.keyWindow?.rootViewController = TabBarController()
}
case .failure(error: let error):
if error.code == 4111 {
Client.delegator.client(client, event: .sessionDidClose(error: error))
} else {
UIAlertController.show(error: error, controller: self)
}
}
})
}
还有,如果使用本地存储usingLocalStorage,这个是持久化存储在Disk里面的吧,随着用户使用次数的增加,会不会越存越多,占用很多存储空间,那如何定期清理一定日期以前存储的内容?