我有2个实体。 1名为WishListElement和其他WishListContainer。
-(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.请帮帮我。