1

I have 2 entities. 1 called WishListElement and other 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];
}

But the problem is that when I try to display the wishlist contents using the above code, it shows an empty table view.

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

The above lines are showing 0. Please help me.

JMS
  • 241
  • 3
  • 14

1 Answers1

1

First of all, create an inverse relationship. As explained here https://stackoverflow.com/a/764572/3429577 it is necessary for data integrity.

Community
  • 1
  • 1
kelin
  • 9,553
  • 6
  • 63
  • 92