【问题标题】:iOS Swift App Memory Issue - Remove Function from Memory After Running?iOS Swift App 内存问题 - 运行后从内存中删除函数?
【发布时间】:2015-02-26 15:57:47
【问题描述】:

第一次提问...我在几所学校安装了一个 iPad Kiosk 模式应用程序。他们有间歇性的互联网问题,而不是应用程序没有响应,我构建了一些代码来检查服务器是否真的可以访问。我尝试了 Reachability.swift 和 Apple 的 Reachability 文件,我也实现了这些文件,但它们并没有走得足够远,无法真正查看互联网连接是否正常......

我的问题是每次代码到达服务器时,都会给内存增加一些开销,最终,内存会填满。我想在函数运行后删除开销,但我似乎找不到如何去做。任何帮助,将不胜感激!

该函数连接到我的服务器,如果应用程序在 5 秒后无法连接,它会计时并抛出错误并显示警报,如果它连接,它什么也不做(除了吃掉一些内存)。服务器上的连接文件是 PHP 并且只包含一个 header

<?PHP header("Content-Type: application/json; charset=UTF-8"); ?>

这是我的代码:

我从 viewDidLoad 调用函数:

override  func viewDidLoad() {
 super.viewDidLoad()

weak var testConnectionTimer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("testConnection"), userInfo: nil, repeats: true)

//WebView UI Here}

//The testing Function

func testConnection(){


        var  connected = 0
        let urlAsString = "https://www.myserver.com/app/online.php"
        let url = NSURL(string: urlAsString)!
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.timeoutIntervalForRequest = 5.0
        let urlSession = NSURLSession(configuration: configuration)


        let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
            if (error != nil) {

            }

            if (error == nil) {
           connected = (connected + 1)

            }
            if (connected != 1)
            {

            var alertController = UIAlertController(title: "MySystem", message: "Can't Connect to Server.  Please Verify Internet Connection and Try Again", preferredStyle: .Alert)

                var okAction = UIAlertAction(title: "Try Again", style: UIAlertActionStyle.Default) {
                    UIAlertAction in
                    self.ActivityMonitor.startAnimating()
                    NSLog("Try Again")
                self.WebView.reload()
                    let isDisplayed = 0
                }
                var cancelAction = UIAlertAction(title: "Exit App", style: UIAlertActionStyle.Cancel) {
                    UIAlertAction in
                    NSLog("App Exited")
                    exit(1)
                }


                alertController.addAction(okAction)
                alertController.addAction(cancelAction)


                    self.presentViewController(alertController, animated: true, completion: nil)


            }

        })

        jsonQuery.resume()
       }

【问题讨论】:

  • 每当您遇到内存问题并且不确定如何继续时,我建议您使用 Instruments。不过,我不确定 Instruments 与 Swift 的配合情况如何,因此请谨慎操作。

标签: php ios json xcode swift


【解决方案1】:

这个视图控制器曾经被解雇过吗?如果是这样,您的重复计时器将阻止它被释放。 NSTimer 保持对其目标的强引用,并且在您调用 invalidate 之前,计时器本身不会被释放。

我建议观看 WWDC 2013 视频 Fixing Memory Issues 和 WWDC 2012 视频 iOS App Performance: Memory,它们向您展示了使用 Instruments 识别泄漏、废弃内存和保留周期的技术。

【讨论】:

    【解决方案2】:

    感谢您的建议!在查看了“工具”之后,它确实是 NSURLSession。我加了

    urlSession.finishTasksAndInvalidate() 
    

    在函数的末尾,这似乎堵住了“洞”

    【讨论】:

      猜你喜欢
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 2016-03-20
      相关资源
      最近更新 更多