iPhone - 核心数据崩溃

时间:2011-10-25 19:19:35

标签: iphone ios ipad core-data

我有两个实体:产品和捆绑产品。每个人都有自己的班级。产品可以是多个捆绑包。

实体的定义如下:

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.

现在,我希望检索属于特定捆绑包的所有产品的列表...

为此,我在Bundle类

上使用此方法
+ (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此时有效。

2 个答案:

答案 0 :(得分:2)

我认为你的谓词是错误的。您没有bundle属性,而是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

答案 1 :(得分:1)

为什么在你有恋爱关系的时候进行抓取?这是沉重而昂贵的。只需通过

请求捆绑产品
[aBundle valueForKey:@"product"];

获取是不必要的,并且当您可能不需要时会强制磁盘命中。核心数据最有可能缓存product关系。

此外,当您将产品分配到捆绑包时,您不需要获取可变设置。只需通过以下方式将捆绑包装入产品:

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

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