一对多的神奇记录

Posted

技术标签:

【中文标题】一对多的神奇记录【英文标题】:Magical record one-to-many 【发布时间】:2015-09-08 10:17:05 【问题描述】:

我正在尝试使用 CoreData 和 Magical 记录。我的人际关系有问题 所以,我有部分,每个部分都应该存储产品 我的部分.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];

能否请您帮助我了解如何添加产品以及以后如何检索它们?谢谢!

【问题讨论】:

作为一个局外人,我喜欢这个标题和介绍。神奇的记录、核心数据和关系问题:P 【参考方案1】:
    首先您需要在编辑 xcdatamodel 文件时将关系设置为一对多

    其次,你应该这样保存数据

    [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。读取一个产品的数据属于一个section。

   //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]];

请随时让我跟进问题

【讨论】:

1)我创建了从部分到产品的关系并设置了一对多 2)当我尝试像这样保存数据时,它没有保存,也许它不正确?当我保存时一切正常 3) NSArray* products = section.products.array;那里没有数组 抱歉,回复晚了。你能贴一张你的 xdatamodel 的图片吗? First 和 Second section 与 products 有关系 - 对很多,product to section 有关系 - to one。对吗? 解决了我在 didSelectRowAtIndexPath 中的问题: Section *section = section[indexPath.row]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"section == %@", section]; NSArray *products = [Product MR_findAllSortedBy:@"name" 升序:YES withPredicate:predicate];

以上是关于一对多的神奇记录的主要内容,如果未能解决你的问题,请参考以下文章

保存一对多实体时出现神奇记录异常

数据库表与表之间的一对一对多多对多关系

sql 一对多的查询

第九节:mybatis关联查询之一对多查询

随一笔:数据库一对多的联表查询

多表查询