From 6deca516ff193e9aa4e4e8c55b848ac2b192f250 Mon Sep 17 00:00:00 2001 From: XHY Date: Tue, 29 Nov 2022 17:06:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20iOS=20=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E5=86=85=E5=AD=98=E8=AD=A6=E5=91=8A=E6=8F=92=E4=BB=B6=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E4=B8=AA=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utssdk/app-ios/index.uts | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/uni_modules/uni-memorywarning/utssdk/app-ios/index.uts b/uni_modules/uni-memorywarning/utssdk/app-ios/index.uts index 2c95ce1..e4dc943 100644 --- a/uni_modules/uni-memorywarning/utssdk/app-ios/index.uts +++ b/uni_modules/uni-memorywarning/utssdk/app-ios/index.uts @@ -3,35 +3,50 @@ import { UIApplication } from "UIKit" import { Selector } from "ObjectiveC" class MemoryWarningTool { - static listener: UTSCallback | null; + static listeners: UTSCallback[] = [] // 监听内存警告 - static listenMemoryWarning(callback: UTSCallback | null) { - this.listener = callback + static listenMemoryWarning(callback: UTSCallback) { - // 注册监听内存警告通知事件及设置回调方法 - // target-action 回调方法需要通过 Selector("方法名") 构建 - const method = Selector("receiveMemoryWarning") - NotificationCenter.default.addObserver(this, selector = method, name = UIApplication.didReceiveMemoryWarningNotification, object = null) + // 只有首次才需要注册监听事件 + if (this.listeners.length == 0) { + // 注册监听内存警告通知事件及设置回调方法 + // target-action 回调方法需要通过 Selector("方法名") 构建 + const method = Selector("receiveMemoryWarning") + NotificationCenter.default.addObserver(this, selector = method, name = UIApplication.didReceiveMemoryWarningNotification, object = null) + } + this.listeners.push(callback) } // 内存警告回调的方法 // target-action 的方法前需要添加 @objc 前缀 @objc static receiveMemoryWarning() { // 触发回调 - this.listener?.({}) + this.listeners.forEach(listener => { + listener({}) + }) } // 移除监听事件 static removeListen(callback: UTSCallback | null) { - this.listener = null - NotificationCenter.default.removeObserver(this) - callback?.({}) + // 移除所有监听 + if (callback == null) { + this.listeners = [] + // 移除监听事件 + NotificationCenter.default.removeObserver(this) + return + } + + // 清除指定回调 + const index = this.listeners.indexOf(callback!) + if (index > -1) { + this.listeners.splice(index, 1) + } } } // 开启监听内存警告 -export function onMemoryWarning(callback: UTSCallback | null) { +export function onMemoryWarning(callback: UTSCallback) { MemoryWarningTool.listenMemoryWarning(callback) } -- GitLab