iPhone - 核心数据崩溃

发布于 2024-12-12 04:27:29 字数 2661 浏览 0 评论 0原文

我有两个实体:产品和捆绑包。每一个都有它的类别。一个产品可以分为多个捆绑包。

实体的定义如下:

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 是有效的。

I have two entities: products and bundles. Each one has its class. A product can be in multiple bundles.

Entities are defined like this:

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

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

Products were assigned to bundle like this:

// 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.

Now I wish to retrieve a list of all products that belong to a specific bundle...

To do that, I am using this method on Bundle class

+ (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;
}

it crashes on the assigned line on the last method with the message "to-many key not allowed here" when I try to do this

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

aBundle is valid at this point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷鸟归林 2024-12-19 04:27:29

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

I think your predicate is wrong. You don't have a bundle property, but a fromBundle property.

If it is really fromBundle, then your predicate should be:

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

EDIT:

If you are trying to do operations on to-many relationships then you'll need to use the aggregate functions for the predicate. I think for your case you'll want the IN operation.

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

濫情▎り 2024-12-19 04:27:29

当你有关系时,为什么要进行取回?那又重又贵。只需通过以下方式请求捆绑包中的产品即可:

[aBundle valueForKey:@"product"];

提取是不必要的,并且在您可能不需要时会强制磁盘命中。核心数据很可能缓存了product关系。

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

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

Core Data 将管理关系的另一方。

Why are you doing a fetch when you have a relationship? That is heavy and expensive. Just request the products for the bundle via

[aBundle valueForKey:@"product"];

The fetch is unnecessary and forces a disk hit when you probably don't need one. Core Data most likely has the product relationship cached.

Also, when you are assigning a product to a bundle you do not need to get a mutable set. Just set the bundle into the product via:

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

Core Data will manage the other side of the relationship.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文