什么时候安全使用

编程入门 行业动态 更新时间:2024-10-12 18:21:46
本文介绍了什么时候安全使用 - [NSManagedObjectContext lock]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道你应该遵守CoreData的线程限制规则,但是是否可以安全地使用 - [NSManagedObjectContext lock] 和朋友?我知道访问 NSManagedObject 属性可以触发隐式 NSManagedObjectContext fetch如果 NSManagedObject 已卸载的属性,所以我假设你必须包装 NSManagedObject 属性访问 - [NSManagedObjectContext lock] 和 - [NSManagedObjectContext unlock] 。我以为这是唯一的。是否有其他人?

I know that you should abide by CoreData's thread confinement rules in general, but is it ever safe to use -[NSManagedObjectContext lock] and friends? I know that accessing an NSManagedObject property can trigger an implicit NSManagedObjectContext fetch if the NSManagedObject has unloaded properties, so I assume you would have to wrap all NSManagedObject property accesses around -[NSManagedObjectContext lock] and -[NSManagedObjectContext unlock]. I thought this was the only gotcha. Are there others?

在此答案的评论中, Marcus Zarra说,我误解了关于 -\ [NSManagedObjectContext lock\] 和朋友的文档:

In the comments of this answer, Marcus Zarra says that I'm misinterpreting the documentation about -\[NSManagedObjectContext lock\] and friends:

将此消息发送到受管对象上下文有助于框架理解多线程环境中的事务范围。最好使用NSManagedObjectContext的NSLocking的实现,而不是使用单独的互斥对象。

Sending this message to a managed object context helps the framework to understand the scope of a transaction in a multi-threaded environment. It is preferable to use the NSManagedObjectContext’s implementation of NSLocking instead using of a separate mutex object.

意味着您可以使用其他锁来保护 NSManagedObjectContext 。这是真的吗?

Also, the above quote implies that you can use other locks to guard NSManagedObjectContext. Is this true?

我不担心这个问题的父/子上下文。

I'm not worried about parent/child contexts for this question.

推荐答案

在学术环境中,你能使用锁吗?是的。

In an academic setting, can you use locks? yes.

你应该在生产代码中使用它们吗?没有。为什么?因为第一次获得正确的几率是非常高的。在维护模式下正确运行会迅速接近零。

Should you ever use them in production code? no. Why? Because the odds of getting it right the first time are extra-ordinarily high. Getting it right in maintenance mode rapidly approaches zero.

使用锁访问Core Data只是要求麻烦。当你弄错了你丢失/损坏的数据。当你得到它正确,你甚至与线密封破碎。

Using locks to access Core Data is just asking for trouble. When you get it wrong you lose/corrupt data. When you get it right you are breaking even with thread confinement. It is a lose/lose gamble with nothing to gain.

最糟糕的部分是,根本没有办法知道你是否正确直到或除非你丢失数据。永远不值得承担风险。

The worst part is that there is virtually no way to know if you got it "right" until or unless you lose data. Never worth the risk.

我还要指向此 Ben的回应,应该给你一些关于这个主题的不错的历史。

I would also point you to this response from Ben which should give you some nice history on this subject.

更多推荐

什么时候安全使用

本文发布于:2023-11-25 15:06:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630229.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:什么时候

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!