如何在核心数据中将非托管对象更改为托管对象

Posted

技术标签:

【中文标题】如何在核心数据中将非托管对象更改为托管对象【英文标题】:How to change non managed object to managed object in core data 【发布时间】:2014-07-02 08:26:25 【问题描述】:

我有非托管对象数据,我想将它们更改为托管对象,但它不起作用。 我在核心数据中有三个以下实体或表格

产品 评论 品牌

Product 是主表,Comment 与 Product 有一对多的关系(1 个产品有很多 cmets),Brand 也与 Product 有一对一的关系。 我通过将非托管对象转换为托管对象,从服务器端获取数据并将这些数据写入这些实体。 我的产品相关数据写入成功,但 cmets 和 Brand 不能。下面是我的代码。产品有实体 SCProduct,评论有 SCComment

在 SCProduct.m 中

-(void)updateWithNMProduct:(NMProduct *)nmProduct

NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;

self.costPrice = [nmProduct.costPrice copy];
self.coverPhoto = [nmProduct.coverPhoto copy];
self.coverPhotoNoSize = [nmProduct.coverPhotoUrlNoSize copy];
self.coverPhotoUrl = [nmProduct.coverPhotoUrl copy];
self.coverPhotoUrlEmail = [nmProduct.coverPhotoUrlEmail copy];
self.createdAt = [nmProduct.createdAt copy];
self.deliveryDays = [nmProduct.deliveryDays copy];
self.deliveryFee = [nmProduct.deliveryFee copy];
self.desc = [nmProduct.desc copy];
self.gender = [nmProduct.gender copy];
self.liked = [nmProduct.liked copy];
self.updatedAt = [nmProduct.updatedAt copy];
self.name = [nmProduct.name copy];
self.numberOfComments = [nmProduct.numberOfComments copy];
self.promoted = nmProduct.promoted;
self.salePrice = [nmProduct.salePrice copy];
self.taxRate = [nmProduct.taxRate copy];


//comments
[self.comments bk_each:^(SCComment *comment)
    [context deleteObject:comment];
];


NSMutableOrderedSet* tempSequence = [NSMutableOrderedSet orderedSetWithOrderedSet:self.comments];
[tempSequence removeAllObjects];
self.comments = tempSequence;


for(NMComment *comment in nmProduct.comments)
    SCComment *managedComment = [SCComment findOrCreate:comment.commentId];
    [managedComment updateWithNonManagedData:comment];
    [self addItem:managedComment toSet:self.comments];

 NMBrand * brand = nmProduct.brand;
    SCBrand *managedComment = [SCBrand findOrCreate:[NSString           stringWithFormat:@"%@",brand.brandId]];
    [managedComment updateWithNonManagedData:managedComment];

在 SCComment.m 中

-(void)updateWithNonManagedData:(NMComment *)nmComment

self.commentId = [nmComment.commentId copy];
self.key = [nmComment.key copy];

SCCommentValue *cValue = [SCCommentValue findOrCreate:nmComment.value.commentValueId];
[cValue updateWithNonManagedData:nmComment.value];
self.value = cValue;

SCCreator *creator = [SCCreator findOrCreate:nmComment.creator.creatorId];
[creator updateWithNonManagedData:nmComment.creator];
self.creator = creator;   

我可以很好地获取产品数据,但不能发表评论。关系很好,并且可以与托管对象一起使用。请帮助我。提前致谢。

【问题讨论】:

您有任何错误吗?您是否在主线程上运行该代码?你在哪里插入 NSManagedObjects 在你的上下文中? 不,没有错误,是的,已经在主线程中完成了所有工作,从服务器获取并写入数据库。我通过调用此方法“saveToPersistentStore”在最后插入,产品正在保存但 cmets 没有。我认为问题可能出在 for(NMComment *comment in nmProduct.cmets) SCComment *managedComment = [SCComment findOrCreate:comment.commentId]; [managedComment updateWithNonManagedData:comment]; [self addItem:managedComment toSet:self.cmets]; 从您的帖子来看,您如何将对象插入 managedObjectContext 并不明显...... findOrCreate 不是标准的 NSManagedObject 方法,而是您的自定义方法。您的其他方法也是如此。那么,如果其中一个有问题,应该如何告知呢? 您能否建议我如何在 NSOrder 类型对象中添加值。如果同样的情况你喜欢 product 和 cmets 如何从 1 到多个,你将如何将值添加到 cmets 中? 我不确定您要问什么……Apples CoreData 文档中涵盖了所有内容……我建议您阅读那里。如果不了解您的类如何实现其所有方法,则无法帮助您解决可能位于代码中某处的问题...尝试识别实际代码中没有按应有的方式运行并放置在这里而不是上面的所有代码...... 【参考方案1】:

在你的方法updateWithNMProduct

-(void)updateWithNMProduct:(NMProduct *)nmProduct

NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;

在创建 context 局部变量后尝试添加这一行...

NSManagedObject *newObject = [context insertNewObjectForEntityForName:@"Product"];

然后使用这个实例来设置实体属性...

例如...

newObject.costPrice = [nmProduct.costPrice copy];

【讨论】:

以上是关于如何在核心数据中将非托管对象更改为托管对象的主要内容,如果未能解决你的问题,请参考以下文章

如何创建/管理多个托管对象上下文?

在托管代码中填充非托管数组

利用IDisposable接口构建包含非托管资源对象

如何创建核心数据 NSManagedObject 的独立实例 - 非托管

核心数据迁移问题:“持久存储迁移失败,缺少源托管对象模型。”

如何在托管类中使用非托管类?