【问题标题】:Core Data syncing in SwiftSwift 中的核心数据同步
【发布时间】:2015-11-20 17:16:20
【问题描述】:

我正在尝试将我刚刚从 json 创建的 NSManagedObject 链接到另一个已保存的 NSManagedObject,但我无法让它工作。

基本上,当我需要显示详细信息时,我的功能如下所示:

func downloadDetails(){context context: NSManagedObjectContext, main:MainEntity, completion () -> Void {

    // First, I'm getting the matching object in the right context 

    if let matchingMain = try context.existingObjectWithID(show.objectID) as? MainEntity {

    // Then, i'm making a request for the details from my main object and 
    // looping inside the json response.
    // Problem: main and matchingMain are wiped out from memory after 
    // the first iteration, and the loop throws an error when i try to make

    fetchDetails()        
    for detailJSON in try detailsJSON.children()  {
       let detail = new DetailFromJson(detailJson)
       // I get an error on the second item because matchingMain data 
       // is no longer here ("data: <fault>")
       detail.main = matchingMain          
    }
    saveContexts()
    completion()
}

我对此很陌生,所以这可能是错误的方式。

但如果有人能解释我如何将数据保留足够长的时间以正确设置它,那就太好了。谢谢。

【问题讨论】:

  • 当询问您在哪里收到错误消息时,您确实需要在问题中包含这些错误消息。它们通常包括关键细节。
  • 一小段可重现的示例代码也将大有帮助。有多个语法错误导致无法知道问题出在哪里,尤其是因为这听起来像是范围/异步问题。
  • 错误不一定是错误,但实际上不可能在没有更多信息的情况下查看您的代码并告诉您出了什么问题。我假设您获取匹配对象的调用不会发生在上下文的队列中。这可以解释解除分配(或至少证明发生了一些奇怪的事情)。

标签: swift core-data nsmanagedobject nsmanagedobjectcontext


【解决方案1】:

/// 更新 ///

好的,事实证明您不能将托管对象作为参数传递并期望它保留在内存中。

我通过在我的函数内部对我的 main 管理发出 fetch 请求解决了这个问题(并在 main 上下文中执行所有操作)。

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2010-10-18
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多