我正在尝试使用CoreData和Magical记录。我的人际关系有问题 所以,我有部分,每个部分都应该存储产品 我的Section.h
@class Product;
@interface Section : NSManagedObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *code;
@property (nonatomic, retain) NSString *sectionId;
@property (nonatomic, retain) Product *product;
@end
@interface Section (CoreDataGeneratedAccessors)
- (void)addProductObject:(Product *)value;
- (void)removeProductObject:(Product *)value;
- (void)addProducts:(NSSet *)values;
- (void)removeProducts:(NSSet *)values;
和Product.h
@interface Product : NSManagedObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *code;
@property (nonatomic, retain) NSString *descrpt;
@property (nonatomic, retain) NSString *imgBig;
@property (nonatomic, retain) NSString *imgSmall;
所以,我试图将产品添加到像这样的部分
for (int i = 0; i<[sectionResponse count]; i++) {
_section = [Section MR_createEntity];
_section.name = [[sectionResponse valueForKey:@"name"] objectAtIndex:i];
_section.code = [[sectionResponse valueForKey:@"code"] objectAtIndex:i];
_section.sectionId = [[sectionResponse valueForKey:@"id"] objectAtIndex:i];
for (int j = 0; j < productResponse.count; j++) {
if ([_section.sectionId isEqualToString:[[productResponse valueForKey:@"section"] objectAtIndex:j]]) {
_product = [Product MR_createEntity];
__product.name = [[productResponse valueForKey:@"name"] objectAtIndex:j];
_product.descrpt = [[productResponse valueForKey:@"desc"] objectAtIndex:j];
[_section addProductObject:_product];
}
}
}
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
是否可以帮助我了解如何添加产品以及如何在以后检索它们?谢谢!
答案 0 :(得分:1)
其次,您应该保存这样的数据
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
.........
for (int i = 0; i<[sectionResponse count]; i++) {
_section = [Section MR_createEntity];
_section.name = [[sectionResponse valueForKey:@"name"] objectAtIndex:i];
_section.code = [[sectionResponse valueForKey:@"code"] objectAtIndex:i];
_section.sectionId = [[sectionResponse valueForKey:@"id"] objectAtIndex:i];
for (int j = 0; j < productResponse.count; j++) {
if ([_section.sectionId isEqualToString:[[productResponse valueForKey:@"section"] objectAtIndex:j]]) {
_product = [Product MR_createEntity];
__product.name = [[productResponse valueForKey:@"name"] objectAtIndex:j];
_product.descrpt = [[productResponse valueForKey:@"desc"] objectAtIndex:j];
// [_section addProductObject:_product];
// Instead of "add Product to Section", you should "set the Product's section"
[_product setSection:_section];
}
}
}
}];
3.因此该部分现在保存为NSSet。读取属于某个部分的产品数据。
//Approach 1
Section* section = [Section MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"......."] inContext:[NSManagedObjectContext MR_defaultContext]];
NSArray* products = section.products.array; //I assume you use "products" as the name
//Approach 2
Product* product = [Product MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"section.sectionID==%@",@"D7689"] inContext:[NSManagedObjectContext MR_defaultContext]];
随时留下我的跟进问题