- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
(如果这个问题有点令人困惑/不精确,我很抱歉。我只是在学习高级 CoreData 用法,我不太了解术语和其他东西)。
我有一个单例 Game
,它包含您在游戏过程中需要的某些数据。例如,您可以从那里访问 currentSite
(Site
是一个 CoreData Entity
)以获取 Site
用户目前位于:
// I created the Site in a background queue (when the game started), then saved the objectID and here I load the objectID
public var currentSiteObjectID: NSManagedObjectID {
let objectIDuri = UserDefaults.standard.url(forKey: Keys.forGameObject.currentSiteObjectIDURI)!
return appDelegate.persistentContainer.persistentStoreCoordinator.managedObjectID(forURIRepresentation: objectIDuri)!
}
// managedObjectContext is the one running on the main queue
public var currentSite: Site! {
return managedObjectContext.object(with: currentSiteObjectID) as! Site
}
你看,我使用 managedObjectContext.object(with:)
方法检索了 currentSite
。
这个方法的文档说:
Returns the object for a specified ID.
If the object is not registered in the context, it may be fetched or returned as a fault. (...)
我不太确定以下内容:
// Each Site has resources that you can access like this
print(Game.shared.currentSite!.resourceSet!.iron)
appDelegate.persistentContainer.performBackgroundTask { (context) in
let currentSite = context.object(with: Game.shared.currentSiteObjectID) as! Site
// Here I increase the iron resource
currentSite.resourceSet!.iron += 42
do {
try context.save()
} catch let error as NSError {
fatalError("\(error.debugDescription)")
}
DispatchQueue.main.async {
print(Game.shared.currentSite!.resourceSet!.iron)
}
}
第二个print
函数正在使用主队列的managedObjectContext
(这与performBackgroundTask {...} 中使用的私有(private)对象不同)
)。
它确实打印了:
50 // the start value
92
我的问题:是否保证 managedObjectContext.object(with:)
返回当前对象(最新的),即使它已在另一个 context 中更改
?文档说,如果它是 context
未知的新对象,它将被获取。但是,如果对象发生变化怎么办?
我不确定上面的示例代码是否按预期工作只是巧合。
感谢您的帮助/解释!我很想了解这类东西。
最佳答案
不,不能保证。如果托管对象已经在上下文中注册,那么它将返回该对象。此外,如果持久存储中不存在具有给定 ID (NSManagedObjectId) 的对象,那么只要您尝试使用其任何属性,您的应用就会崩溃。
关于ios - 是否 managedObjectContext.object(with :) always refetch data if another (private) managedObjectContext changed and saved it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42556010/
我正在从tangowithdjango学习django 。我试图理解 populate_rango.py 的代码。代码是: import os os.environ.setdefault('DJANG
我试图理解Rserve参数--save,-no-save和--vanilla之间的区别。我在文档或任何论坛中都没有看到任何描述这些效果的内容。有谁确切地知道这些是做什么的? 在OSX中,我需要指定其中
我正在使用 CoreData 制作一个基于文档的应用程序。我可以创建一个新文档,编辑该文档,然后保存它。文件已创建并可以打开。打开后,数据会正确加载。但是,一旦进行了初始保存,所有后续保存都不会执行任
下面提出了类似的问题 How to save complete web page 但目前还没有答案。预期的结果是得到很多文件,一些文件来存储图像等。 我使用了以下内容,它会弹出一个窗口说保存文件 va
我们一直在测试一种不同的保存方式。然而,结果并不像我们预期的那样。我们有创建调查的方法,每个调查有多个问题。我们测试了几个案例,它们都以相同的方式提交查询。 @Transactional class
我想了解JAP Repotitoty的详细信息。我创建了一个服务类、实体类和存储库类,如下所示(用 kotlin 编写)并执行了 ItemService#update 方法。 执行 item2Repo
我正在开发我的第一个 Firefox 扩展。我正在尝试将数据保存在浏览器的本地存储中(使用 Window.localStorage 很容易,但我正在关注 official recommandation
这让我很郁闷。我是 C Sharp 的新手,因此需要一些帮助。我的保存/另存为完全是胡说八道。 真的有两个问题: 如何在不弹出保存对话框的情况下保存对现有文件的更改?如果我单击“保存”,它会弹出一个对
我有一个代码可以将 XML 文件保存到我的目录中。它在我的本地主机和我的共享主机中实际上就像一个魅力,但它在我的 Linux VPS 中不起作用。 我总是遇到这个错误: 警告:DOMDocument:
有没有办法在 django 管理站点中同时“另存为”和“保存并添加另一个”? 最佳答案 我不认为按钮引用的 URL 有任何神奇之处,因此您可以通过简单地覆盖每个 http://docs.djangop
创建 playramework 的模型时,我们可以使用 save() 或 _save() 方法。为什么这两种方法在框架中都可用,原因是什么? (在这种情况下,他们做同样的事情 - 将对象保存到数据库)
我见过两个都调用 $save 的代码和 save到 $resource 的 Angular 。 有什么区别,你什么时候使用? 最佳答案 最佳解释===例子 : // by writing '{ id:
根据save bang your head, active record will drive you mad ,在特殊情况下我们应该避免使用 save! 和 rescue 习惯用法。鉴于此,假设模型
我的菜单栏中有两个按钮,其中包含“保存”和“另存为”按钮。但是,我目前拥有它们相同的代码,并且它会按当前方式保存,并提示用户要保存在哪里。我希望保存按钮仅保存而不提示对话框,除非文件尚不存在。 我尝试
我知道 models.Model 和 forms.ModelForm 都包含您可以覆盖的 .save() 方法。我的问题是它们如何以及何时用于保存对象以及以什么顺序。 最佳答案 ModelForm.s
我一直在尝试使用 freeze_graph函数来获取模型+权重/偏差,但在这个过程中,我发现我的初始网络似乎没有任何变量,尽管能够正确分类图像。我的代码如下: #!/usr/bin/python im
尝试使用 gTTS 模块将文本转换为语音并另存为 wav 文件。 我的代码: import gTTS text = "This is my text in the saving folder" tts
我有一个包含大约 50 个字段和两个提交按钮的表单,“保存”和“保存并提交”。如果用户单击“保存”,则插入用户在表格中填写的值。当用户单击“保存并提交”按钮时,它应该更新或插入用户在表单中填写的所有字
我是 Django 新手。我想知道 django 中的 ModelForm 和 Model 如何协同工作?我的意思是 ModelForm.save() 如何自动保存与之关联的模型?它如何从 reque
我有亲子关系: @Entity @Table(name = "user") public final class User { @Id @GeneratedValue(strategy
我是一名优秀的程序员,十分优秀!