2014-09-30 113 views
1

我有2個實體。 1稱爲WishListElement和其他WishListContainer。從核心數據中的一對多關係中獲取實體數據

CoreData

-(BOOL)addElementToWishList:(WishListElement*)element 
{ 
    ASAppDelegate* appDelegate = [UIApplication sharedApplication].delegate; 
_managedObjectContext = [appDelegate managedObjectContext]; 

WishListElement *wishList = [NSEntityDescription insertNewObjectForEntityForName:@"WishListElement" inManagedObjectContext:_managedObjectContext]; 

[wishList setAppName: element.appName]; 
[wishList setAppPrice: element.appPrice]; 
[wishList setAppCategory: element.appCategory]; 
[wishList setAppSummary: element.appSummary]; 
[wishList setAppCopyright:element.appCopyright]; 
[wishList setAppAuthor: element.appAuthor]; 
[wishList setAppImage:element.appImage]; 

NSError *error = nil; 
[_managedObjectContext save:&error]; 

WishListContainer *wishListContainer = [NSEntityDescription insertNewObjectForEntityForName:@"WishListContainer" inManagedObjectContext:_managedObjectContext]; 
[wishListContainer addContainsObject:wishList]; 

if (![_managedObjectContext save:&error]) 
{ 
    return NO; 
} 
else 
{ 
    return YES; 
} 
} 

-(NSMutableArray*)getWishListElement 
{ 
ASAppDelegate *appDelegate = (ASAppDelegate*) [[UIApplication sharedApplication]delegate]; 
_managedObjectContext = [appDelegate managedObjectContext]; 


NSFetchRequest *request = [[NSFetchRequest alloc]init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"WishListContainer" inManagedObjectContext:_managedObjectContext]; 
[request setEntity:entity]; 

NSError *error = nil; 
NSMutableArray *fetchRequest = [[_managedObjectContext executeFetchRequest:request error:&error]mutableCopy]; 

[self setWishListArray:fetchRequest]; 

WishListContainer *container = [fetchRequest objectAtIndex:0]; 
NSLog(@"%d",container.contains.count); 
return [container.contains.allObjects mutableCopy]; 
} 

但問題是,當我嘗試使用上述代碼以顯示心願內容,則顯示一個空表視圖。

WishListContainer *container = [fetchRequest objectAtIndex:0]; 
NSLog(@"%d",container.contains.count); 
return [container.contains.allObjects mutableCopy]; 

上面的行顯示0.請幫助我。

+0

你使用反比關係嗎? – Maul 2014-09-30 09:21:58

+0

不,我不使用反比關係。 – JMS 2014-09-30 09:42:04

回答