【问题标题】:NSData - Which is better in terms of memory usage: initWithContentsOfURL or NSURLConnectionNSData - 在内存使用方面哪个更好:initWithContentsOfURL 或 NSURLConnection
【发布时间】:2009-02-12 06:37:53
【问题描述】:

我想从 URL 中获取 NSData 对象的内容。就内存使用 dataWithContentsOfURL(或 initWithContentsOfURL)或使用 NSURLConnection 而言,更有效的方法是什么?

我应该使用

NSData *data = [[NSData alloc] initWithContentsOfURL:myURL]

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

【问题讨论】:

    标签: iphone memory-management


    【解决方案1】:

    我不知道 Apple 代码的内部结构,但我猜想 NSData 的 initWithContents 的 URL 在内部使用 NSURLConnection。内存使用差异可以忽略不计。

    使用 NSURLConnection 的异步 api 可以让您通过处理传入的数据来提高内存效率,但是(不知道您实际在做什么)我认为这是一个相当激进的优化,您应该离开,直到您开始工作代码。

    【讨论】:

      【解决方案2】:

      很确定它们是相当的。

      【讨论】:

      • 你是在告诉 "initWithContentsOfURL" "sendSynchronousRequest" 使用同步连接吗?
      • initWithContentsOfURL: 不会触发委托方法或接受回调块,因此它必须是同步的。
      猜你喜欢
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2018-11-05
      • 2014-03-24
      相关资源
      最近更新 更多