Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.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 使用MagicalRecord插入后CoreData关系未更新_Ios_Core Data_Swift_Magicalrecord - Fatal编程技术网

Ios 使用MagicalRecord插入后CoreData关系未更新

Ios 使用MagicalRecord插入后CoreData关系未更新,ios,core-data,swift,magicalrecord,Ios,Core Data,Swift,Magicalrecord,我有一个有两个视图的应用程序。第一个允许在核心数据中创建多个实体,第二个允许显示所创建实体的数据 在我的CoreData数据模型中,我有两个实体: CD_产品和CD_产品项 CD_AProduct包含名为productItems的可选关系,这些关系是CD_AProductItems: CD_AProductItems包含一个名为product的非可选关系,即CD_AProduct 在AppDelegate类中,我按如下方式实例化核心数据堆栈: MagicalRecord.setupAutoMig

我有一个有两个视图的应用程序。第一个允许在核心数据中创建多个实体,第二个允许显示所创建实体的数据

在我的CoreData数据模型中,我有两个实体:

CD_产品和CD_产品项

CD_AProduct包含名为productItems的可选关系,这些关系是CD_AProductItems:

CD_AProductItems包含一个名为product的非可选关系,即CD_AProduct

在AppDelegate类中,我按如下方式实例化核心数据堆栈:

MagicalRecord.setupAutoMigratingCoreDataStack()
第一个视图的代码

然后在我的代码中,我第一次创建了四个CD_产品项,并链接了这些关系:

if let entity = CD_ProductItem.MR_createEntity() as? CD_ProductItem
{
    product.addProductItemsObject(entity)

    entity.product = product

    success()
}
else
{
    failure()
}
然后,我将实体保存在持久存储中:

NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreWithCompletion({(successBlock : Bool, error : NSError!) in

        if successBlock
        {
            success()
        }
        else
        {                            
            if error != nil
            {
                failure()
            }
            else
            {
                success()
            }
        }
    }
)
第二个视图的代码

这有什么问题吗

第一视图:用户选择4个产品项添加到产品中。 单击“我的视图”的右上角按钮转到下一个视图。 第二视图:显示创建的4个产品项的数据。 使用“我的视图”的左上角按钮返回到第一页。 第一视图:在我的产品中添加4个以上的产品项 转到下一页 第二视图:仅显示4个产品项,而不是8个! 我认为这个问题来自核心数据堆栈,当我转到第二个视图时,它没有更新,该视图显示所选产品项的数据


关于发生了什么以及如何确保在转到第二个视图之前已更新整个核心数据堆栈,有什么建议吗?

据我所知,您需要在不同的indexPath中使用不同的元素,因此在这种情况下,您应该在转到下一个viewController之前提供要找到的indexPath。此时,您正在加载包含product.productItems的所有项目。由于您的帖子中只有少量代码,让我们设想您将UITableView作为第一个控制器

首先,您需要创建一个数组来存储CD_a产品元素:

var productArrays = [CD_AProduct]!
您具有保存功能,因此我们将跳过此步骤

然后只需创建简单的prepareForSegue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   if (segue.identifier == "GoToDetailView") {
      let vc = segue.destinationViewController as! CD_AProductItemsView
      let CD_AProduct = productArrays[tableView.indexPathForSelectedRow()!.row]
      vc.CD_AProductDetailView = CD_AProduct
      vc.CD_AProductItemsDetailView = productArrays.detailEntities.allObjects as! Array
  }
}
最后一行detailEntities中的p.S表示托管对象列表中的NSSet。你应该改变这个

现在,如果运行应用程序,它将崩溃,因为此时tableView.indexPath将为零,因为没有人将此信息提供给prepareForSegue。因此,要解决此问题,我们应该使用我们的DidSelectRowatineXpath:

我们完了!答案当然很晚了,但也许它会帮助一些人

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   if (segue.identifier == "GoToDetailView") {
      let vc = segue.destinationViewController as! CD_AProductItemsView
      let CD_AProduct = productArrays[tableView.indexPathForSelectedRow()!.row]
      vc.CD_AProductDetailView = CD_AProduct
      vc.CD_AProductItemsDetailView = productArrays.detailEntities.allObjects as! Array
  }
}
self.performSegueWithIdentifier("GoToDetailView", sender: self)