Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如果另一个(私有)managedObjectContext更改并保存了数据,managedObjectContext.object(带:)是否总是重新提取数据?_Ios_Swift_Core Data_Nsmanagedobjectcontext - Fatal编程技术网

Ios 如果另一个(私有)managedObjectContext更改并保存了数据,managedObjectContext.object(带:)是否总是重新提取数据?

Ios 如果另一个(私有)managedObjectContext更改并保存了数据,managedObjectContext.object(带:)是否总是重新提取数据?,ios,swift,core-data,nsmanagedobjectcontext,Ios,Swift,Core Data,Nsmanagedobjectcontext,(如果这个问题有点让人困惑/不精确,我很抱歉。我只是在学习高级CoreData用法,我对术语和内容不太了解) 我有一个单件游戏,它保存了游戏中需要的某些数据。例如,您可以从那里访问当前站点(站点是一个核心数据实体),以获取用户当前所在的站点: // I created the Site in a background queue (when the game started), then saved the objectID and here I load the objectID public

(如果这个问题有点让人困惑/不精确,我很抱歉。我只是在学习高级CoreData用法,我对术语和内容不太了解)

我有一个单件
游戏
,它保存了游戏中需要的某些数据。例如,您可以从那里访问
当前站点
站点
是一个核心数据
实体
),以获取用户当前所在的
站点

// 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

该方法的文档说明:

返回指定ID的对象

如果对象未注册 在上下文中,它可以作为错误获取或返回。(……)

我不太确定以下几点:

// 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{…}
中使用的私有函数不同)

它实际上打印:

50 // the start value
92 
我的问题是:
managedObjectContext.object(with:)
是否保证返回当前对象(即最新对象),即使它已在另一个
上下文中更改过?文档中说,如果它是
上下文不知道的新对象,将获取它。
但是如果一个对象发生了变化怎么办

我不确定上面的示例代码是否像预期的那样工作只是巧合


谢谢你的帮助/解释!我很想了解这种东西。

不,这不是保证。如果托管对象已在上下文中注册,则它将返回此对象。此外,如果永久存储中不存在具有给定id(NSManagedObjectId)的对象,则您的应用程序将在尝试使用其任何属性时崩溃。

但我在更改值之前使用了打印语句。这是否意味着managedObject已在上下文中注册?执行之后,它会给我更新的对象。此外,我确保它是一个有效的objectID。您是否持有对托管对象的强引用?仅当对象有一些更改未保存到永久存储库时,上下文才保持强引用我不知道。如果您持有对对象的强引用,则此对象将不会解除分配,因此对象将在上下文中注册(除非在更新后合并上下文,否则您将看不到其他上下文中的任何更改)。如果您不持有强引用(并且对象没有任何挂起的更改),则ARC将解除锁定它,并将从上下文中注销它,这样您将看到其他上下文中的所有更改(如果更改保存到持久存储),因为新的“版本”将从持久存储中获取好的,所以“managedObjectContext.object”(带:)只有在我对“站点”对象没有强引用的情况下,才会提供最新视图,对吗?但是,有没有更方便的方法来获取主上下文的最新版本?