2011-10-25 8 views
0

商品とバンドルの2つのエンティティがあります。それぞれにはそのクラスがあります。製品は複数のバンドルにすることができます。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 *allProductsInBundle = [Bundle ProductsInBundle:aBundle inManagedObjectContext:self.managedObjectContext]; 

aBundleをしようとすると、「鍵がここに許可されないように、多くの」メッセージとの最後の方法で割り当てられたラインにクラッシュする10

は、この時点では有効です。

+0

こちらの質問には、「Tabuleiros」はコピー/ペーストミスですか?編集:あなたはすでにそれを修正したように見える^ _^ – dontGoPlastic

+0

ちょうどタイプミス。 :D – SpaceDog

+0

Bundleクラスの作成方法は? (すなわち、実際のクラスヘッダと実装では、コアデータを生成していますか?) – hypercrypt

答えて

2

あなたの述語が間違っていると思います。バンドルプロパティはありませんが、fromBundleプロパティはありません。

それは本当にfromBundleであれば、あなたの述語は次のようになります。

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

EDIT:

あなたは対多の関係で操作をしようとしているなら、あなたは、集計を使用する必要があります述語のための関数。あなたの場合、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"]; 

経由バンドルの製品を要求することは不要であり、あなたはおそらく1を必要としないとき、ディスクヒットを強制します。コアデータには、おそらくproductの関係がキャッシュされています。

また、バンドルに製品を割り当てるときに、変更可能なセットを取得する必要はありません。

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

コアデータは、関係の反対側を管理します。

関連する問題