2011-10-25 168 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"]; 

核心數據將管理關係的另一方。