iOS Safari内存泄漏导致应用程序无法使用

iOS Safari memory leak makes Apps unusable

本文关键字:应用程序 Safari 内存 泄漏 iOS      更新时间:2023-09-26

我的web应用程序中有一个"可编辑表"。它的工作原理是,当你点击表的td时,我读取td的innerHTML,并生成一个输入字段,我用innerHTML的值将其注入td。

当您模糊/聚焦输入字段时,我会破坏输入字段并设置新值的innerHTML,并发送AJAX请求来更新该字段。这在台式机和安卓手机上非常适用,但在iOS Safari上,你编辑的值越多,速度就会越来越慢。经过大约250次编辑后,滚动将变得非常不稳定,最终页面将变得几乎不可用。页面刷新并不能解决问题,它要求您完全关闭网页并重新开始。

这让我相信Safari并没有释放被破坏的输入字段的内存。

下面给出了相关的JavaScript代码,点击td:即可启动

_handleEditing: function($td) {
    var value = $td.find("span").html().replace(",", ""),
        ctrl = this;
    $td.unbind('click');
    // Clone the input control
    var $editControl = $('#input .edit-control').clone();
    if(this.isMobile) {
        $editControl.attr('type', 'number');
        $editControl.attr('step', '0.01');
    }
    // Inject editControl into td html and set appropriate values
    $td.html($editControl);
    $editControl.val(value);
    // Focus the edit control and set the selection range
    $editControl.focus();
    setTimeout(function() {
        $editControl.get(0).setSelectionRange(0, value.length);
    }, 0);
    firstKeydown = true;
    // Handle leaving the input field and returning to normal number
    $editControl.on('blur', function(){
        var newValue = $editControl.val();
        // If the newValue is empty, go back to the old value
        newValue = (newValue === '') ? value : newValue;
        var parsedNewValue = parseFloat(newValue).toFixed(2);
        // If the newValue is not actually a number, go back to the old value
        newValue = isFinite(parsedNewValue) ? parsedNewValue : value;
        $editControl.unbind().remove();
        $editControl = null;
        $td.html("<span>"+newValue+"</span>");
        // Queue this value update to server
        if(parseFloat(newValue) !== parseFloat(value)) {
            ctrl.handleValueChanged($td, value, newValue);
        }
        // Reinstate the click handler on the td
        $td.click(function(){
            ctrl._handleEditing($td);
        });
    });
    this._attachNavigationHandlers($editControl);
},

任何帮助都将不胜感激。苹果开发者请帮助

根据我对类似问题所做的研究(iOS上的性能逐渐下降),我发现了许多来源表明这是iOS本身的问题。

iOS 9中的性能问题似乎与Safari中的一个错误有关。无论您使用的是Safari、UIWebView还是WKWebView。。。每次选择文本输入字段时,您都会注意到性能的提高。我创建了一个包含100个文本输入的基本html页面。。。当我浏览输入几次时,应用程序最终对手势没有反应。Webview/Webkit中唯一的解决方案(据我所知)是重新启动应用程序。

参考:https://forums.developer.apple.com/thread/21956

到目前为止,似乎还没有解决方案。在我的情况下,当用户注销时,我会强制关闭应用程序。不幸的是,如果你想让你的应用程序出现在应用商店中,你无法通过编程退出你的应用。