我有非托管对象数据,我想将它们更改为托管对象,但它无法正常工作。 我在核心数据中有三个以下实体或表
产品是主表,评论连接到具有一对多关系的产品(1个产品有很多评论),品牌也连接到产品与一对一的关系。 我从服务器端获取数据,并通过将非托管对象转换为托管对象将这些数据写入这些实体。 我的产品相关数据写得很成功,但评论和品牌无法做到。以下是我的代码。产品有实体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; }
我能够获得产品的数据,但不能评论。关系很好并且与托管对象一起使用。请帮助我。提前谢谢。
答案 0 :(得分:0)
在方法updateWithNMProduct
-(void)updateWithNMProduct:(NMProduct *)nmProduct{
NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
创建context
局部变量后,尝试添加此行...
NSManagedObject *newObject = [context insertNewObjectForEntityForName:@"Product"];
然后使用此实例设置实体属性...
例如......
newObject.costPrice = [nmProduct.costPrice copy];