2011-10-25 78 views
0

我有两个实体:产品和捆绑。每个人都有自己的班级。一个产品可以有多个捆绑。iPhone - 核心数据崩溃

实体的定义是这样的:

PRODUCTS 
name, string 
number, integer 16 
fromBundle = to-many relationship to product 

BUNDLE 
name, string 
number, integer 16 
product = to-many relationship to fromBundle 

产品被分配到捆绑这样的:

// suppose bundle 1 is composed of products 1, 2, 3 and 4. 
NSArray *myProd = [NSArray arrayWithObjects: 
    [NSNumber numberWithInt:1], 
    [NSNumber numberWithInt:2], 
    [NSNumber numberWithInt:3], 
    [NSNumber numberWithInt:4], 
    nil]; 

int bundleNumber = 1; 
NSString *bundleName = @"My Bundle"; 
Bundle *aBundle = nil; 

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 

request.entity = [NSEntityDescription entityForName:@"Bundle" inManagedObjectContext:context]; 
request.predicate = [NSPredicate predicateWithFormat: @"(number == %d)", bundleNumber]; 
NSError *error = nil; 
aBundle = [[context executeFetchRequest:request error:&error] lastObject]; 

// as the bundle does not exist, this will run 
if (!error && !aBundle) { 
    aBundle = [NSEntityDescription insertNewObjectForEntityForName:@"Bundle" inManagedObjectContext:context]; 
    aBundle.string = bundleName; 
    aBundle.Number = [NSNumber numberWithInt:bundleNumber]; 

    for (NSNumber *umNum in myProd) { 

      // the product with number = aNum is retrieved... yes it is valid at this point 
      Product *oneProduct = [ProductWithNumber:umNum inManagedObjectContext:context]; 
      NSMutableSet *mutableSet = [oneProduct mutableSetValueForKey:@"fromBundle"]; 
      [mutableSet addObject:aBundle]; 
    } 
// Save the context. 
NSError *error = nil; 
if (![context save:&error]) { 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
} 

// everything is fine at this point. 

我现在想找回那属于一个特定的捆绑所有产品的列表..

要做到这一点,我在捆绑类上使用此方法类

+ (NSArray *)ProductsInBundle:(Bundle*)aBundle inManagedObjectContext:(NSManagedObjectContext *)context 
{ 
    NSArray *all = nil; 


    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 
    request.entity = [NSEntityDescription entityForName:@"Products" inManagedObjectContext:context]; 
    request.predicate = [NSPredicate predicateWithFormat:@"(fromBundle == %@)", aBundle]; 

    NSError *error = nil; 
    all = [context executeFetchRequest:request error:&error]; // crashes here 

    return all; 
} 

崩溃与“一对多这里不允许键”当我试图做到这一点

NSArray *allProductsInBundle = [Bundle ProductsInBundle:aBundle inManagedObjectContext:self.managedObjectContext]; 

aBundle消息的最后一个方法所分配行是在这一点上有效。

+0

“Tabuleiros”在这里只是您的问题的复制/粘贴错误吗?编辑:看起来像你已经修好它^ _^ – dontGoPlastic

+0

只是一个错字。 :D – SpaceDog

+0

你是如何创建Bundle类的? (即实际的类头和实现,你有核心数据为你吗?) – hypercrypt

回答

2

我认为你的谓词是错误的。您没有捆绑软件属性,而是一个fromBundle属性。

如果真的是fromBundle,那么你的谓语应该是:

equest.predicate = [NSPredicate predicateWithFormat:@"(fromBundle == %@)", aBundle]; 

编辑:

如果你正在尝试做的一对多关系操作,那么你需要使用聚合谓词的函数。我认为对于您的情况,您需要IN操作。

http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html#//apple_ref/doc/uid/TP40001795-215891

+0

对不起,这是一个错字,因为我不得不修改代码在这里发布,以使其更清楚。我已纠正它。 – SpaceDog

+0

看到我的编辑可能的解决方案 – logancautrell

+0

谢谢。而已! – SpaceDog

1

你为什么这样做一个获取当你有关系吗?这是沉重而昂贵的。只是要求产品通过捆绑通过

[aBundle valueForKey:@"product"]; 

取回是不必要的,并强制磁盘命中,当你可能不需要一个。核心数据最有可能具有缓存的product关系。

另外,当您将产品分配到捆绑包时,您不需要获取可变集合。只需通过以下步骤将产品捆绑到产品中:

[product setValue:bundle forKey:@"fromBundle"]; 

核心数据将管理关系的另一方。