如何在 iOS 中停止 JSContext 评估?
Posted
技术标签:
【中文标题】如何在 iOS 中停止 JSContext 评估?【英文标题】:How to stop JSContext evaluating in iOS? 【发布时间】:2018-06-26 01:39:01 【问题描述】:我需要在 [context evaluateScript:js]
运行时中断它,我在文档和 Google 中找不到方法。有人可以帮忙吗?
【问题讨论】:
一种天真的方法,我猜,你可以从 javascript 调用一些 Objective-C 方法,它会通知 JS 代码(返回 bool 值)它应该停止执行。并定期调用此方法并从 JS 中止执行。 @BorysVerebskyi evaluateScript 方法像盲目地运行 js,直到脚本自行停止。 【参考方案1】:我无法使用JavaScriptCore
让它工作。我试过了:
DispatchWorkItem
中并调用cancel()
将代码包装在 Operation
中并尝试取消它以及 nil
'ing OperationQueue
(这实际上会导致应用崩溃)。
使用JavaScriptCore
中的C API 以及-fno-objc-arc
标志来手动管理内存。
这些都不起作用。
所做的工作是使用WKWebView
,一旦nil
'ed,evaluateJavaScript
函数就会调用其完成处理程序。我发布了一个示例here。
对于任何尝试这样做的人,请记住这是未记录的行为,因此它可能会在任何即将推出的 ios 版本中发生变化。
【讨论】:
如果需要返回更多上下文结果。将 webView 的 WKScriptMessageHandler 设置为 WKUserContentController 并在 deinit 时删除处理程序。【参考方案2】:如果您使用JSContext
的evaluateJavaScript
方法。由于上下文上没有取消方法,因此您无法停止它。而且iOS不喜欢macOS可以使用XPC所以没办法停止外部运行环境。
但是,有一个解决方法。想想JSContext
喜欢浏览器。剧本在路上飞。如何通过外部事件阻止它。最方便的方法是自己构建runloop。将任务变小并放入队列中。
自定义运行循环
我每隔几秒导出一个本机 Timer 以在 downloadWithHandler 方法上调用 javascript 处理程序,并在某个时候将 isCancelled
设置为 true。 runloop 将停止,JSContext eval 到达整个脚本的末尾并返回。
本机:
let script = viewModel.string
let context = EAJSContext()!
// context will be dealloc after finish if not retain here.
// not retain it to debugging
// viewModel.context = context
DispatchQueue.global(qos: .background).async
// inject helper metholds
JavaScriptCoreHelper().config(context: context)
// export download(handler:) method
Emulator.configure(context: context)
let result = context.evaluateScript(script)
print("context return result: \(result)")
脚本:
var emulator = Emulator.create();
var taskIsFinish = false;
const promiseA = new Promise( (resolve, reject) =>
emulator.downloadWithHandler( (response) =>
// native pass dictionary
var isFinish = response["isFinish"];
var isCancelled = response["isCancelled"];
var date = response["date"];
console.log("isFinish: " + isFinish);
console.log("isCancelled: " + isCancelled);
console.log("date: " + date);
if (isFinish || isCancelled)
resolve(date);
);
);
promiseA
.then( (val) =>
console.log("asynchronous logging has val: " + val)
taskIsFinish = true
)
while(!taskIsFinish)
// long task in queue
sleep(1);
console.log('long task');
输出:
2020-05-10 04:07:06.766592+0800 …[34354:2831281] [interaction] … runToolbarItemPressed(_:): run triggerd
console.log: isFinish: false
console.log: isCancelled: false
console.log: date: Sun May 10 2020 04:07:06 GMT+0800 (CST)
console.log: isFinish: false
console.log: isCancelled: false
console.log: date: Sun May 10 2020 04:07:06 GMT+0800 (CST)
console.log: long task
console.log: isFinish: false
console.log: isCancelled: false
console.log: date: Sun May 10 2020 04:07:07 GMT+0800 (CST)
console.log: long task
console.log: isFinish: false
console.log: isCancelled: false
console.log: date: Sun May 10 2020 04:07:08 GMT+0800 (CST)
console.log: long task
console.log: isFinish: false
console.log: isCancelled: false
console.log: date: Sun May 10 2020 04:07:10 GMT+0800 (CST)
console.log: long task
console.log: isFinish: false
console.log: isCancelled: false
console.log: date: Sun May 10 2020 04:07:11 GMT+0800 (CST)
console.log: long task
console.log: isFinish: false
console.log: isCancelled: true
console.log: date: Sun May 10 2020 04:07:12 GMT+0800 (CST)
console.log: asynchronous logging has val: Sun May 10 2020 04:07:12 GMT+0800 (CST)
console.log: long task
console.log: isFinish: false
console.log: isCancelled: true
console.log: date: Sun May 10 2020 04:07:13 GMT+0800 (CST)
context return result: Optional(undefined)
console.log: isFinish: false
console.log: isCancelled: true
console.log: date: Sun May 10 2020 04:07:13 GMT+0800 (CST)
console.log: isFinish: false
console.log: isCancelled: true
console.log: date: Sun May 10 2020 04:07:13 GMT+0800 (CST)
2020-05-10 04:07:13.391950+0800 …[34354:2831310] [debug] EAJSContext.swift[16], deinit
(如果在 eval 完成后上下文没有 deinit,内存可能会泄漏。)
其他想法
将上下文包装在本地 Worker 类中。并设置标志以使该工作人员在任务取消时以空结果快速返回回调。请勿保留和丢弃。
【讨论】:
以上是关于如何在 iOS 中停止 JSContext 评估?的主要内容,如果未能解决你的问题,请参考以下文章
有啥方法可以停止或暂停 JSContext 对象中 Javascript 的执行?
获取字符串的值类型(JSContext evaluateScript)
swift 笔记:iOS与JavaScript的交互(二):JavaScriptCore:16。 JSContext相关演示,在委托:webViewDidFinishLoad()中获取上下文