使用“哈希”是否安全?跟踪内存中 NSURLConnections 的方法?
我的问题与此讨论相关: http ://www.cocoabuilder.com/archive/cocoa/202211-how-to-get-nsurl-form-nsurlconnection.html
我正在发送几个 NSURLConnections 来传输数据,并且需要能够判断哪个特定连接失败或成功。有一些 NSURLDelegate 方法(didFailWithError 等)返回相关的 NSURLConnection。问题是委托方法中没有返回 NSURLRequest (并且 NSURLConnection 中没有 NSURL 访问器)。
我实现的解决方案是维护一个 NSMutableDictionary,它将发送的 URL 字符串与此 NSURLConnection 的“hash”方法的结果配对。
我已经测试过它,它似乎有效 - 委托方法中返回的 NSURLConnection 的哈希值与最初发送的 NSURLConnection 的哈希值相同。
我的问题:这样做安全吗?有比哈希更好的密钥吗?我问这个问题是因为在我天真的理解中,哈希值在某种程度上与内存中该对象的地址相关联,并且当应用程序被重写到内存时,后台应用程序或关闭和打开手机似乎可能会改变这个值。
非常感谢!
My question is related to this discussion: http://www.cocoabuilder.com/archive/cocoa/202211-how-to-get-nsurl-form-nsurlconnection.html
I am sending several NSURLConnections to transmit data and need to be able to tell which specific connection failed or succeeded. There are NSURLDelegate methods (didFailWithError, etc) which return the related NSURLConnection. The problem is that there is no NSURLRequest returned in the delegate methods (and there is no NSURL accessor in the NSURLConnection).
The solution I've implemented is to maintain a NSMutableDictionary which pairs the URL string that was sent with the result of this NSURLConnection's "hash" method.
I've tested it and it seems to work - the hash of the NSURLConnection that is returned in the delegate method is the same as the hash from the NSURLConnection that was sent initially.
My question: is it safe to do this? Is there a better key to use than the hash? I'm asking because in my naive understanding, the hash is somehow associated with the address of that object in the memory, and it seems possible that backgrounding the app or turning the phone off and on may change this value as things are rewritten to memory.
Thanks very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常使用 ASIHTTPRequest,当发出多个连接(同时在队列或并行中)时,我使用 userInfo 字典来传递上下文。
您引用的“哈希 IVAR”实际上是在
NSObject
协议中定义的 - 作为一种方法。它旨在用于哈希表,因此应该足以满足您的需求。我仍然更喜欢用更一流的方法来解决这个问题,同时更明确哪个请求正在完成/出错。
I generally use
ASIHTTPRequest
, and when issuing multiple connections (concurrently in queue or parallel) I use the userInfo dictionary to pass around context.The "hash IVAR" you refer to is actually defined in the
NSObject
protocol - as a method. It is intended to be used in a hash table, and as such should be sufficient for your needs.I'd still prefer a more first-class approach to this problem, whilst making it more explicit which request is finishing/erroring-out.