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.
现在我希望检索属于特定捆绑包的所有产品的列表...
为此,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的谓词是错误的。您没有捆绑属性,但有 fromBundle 属性。
如果它确实是 fromBundle,那么您的谓词应该是:
编辑:
如果您尝试对多对多关系进行操作,那么您需要使用谓词的聚合函数。我认为对于您的情况,您需要
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:
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
当你有关系时,为什么要进行取回?那又重又贵。只需通过以下方式请求捆绑包中的产品即可:
提取是不必要的,并且在您可能不需要时会强制磁盘命中。核心数据很可能缓存了
product
关系。此外,当您将产品分配给捆绑包时,您不需要获取可变集。只需通过以下方式将捆绑包设置到产品中:
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
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:
Core Data will manage the other side of the relationship.