简体   繁体   English

具有关系的核心数据明细视图

[英]Core Data Detail View with relationship

I have set up a 1 to many relationship on my core data entities. 我在核心数据实体上建立了一对多的关系 I am trying to show the detailview copy of the associated data. 我试图显示相关数据的detailview副本。 Currently I have the prepareforseague: method working with the original entity(Routines), however I am at a lose as to how to show the linked entity (RoutinesDetails). 目前,我有prepareforseague:方法可与原始实体(Routines)一起使用,但是我对如何显示链接的实体(RoutinesDetails)感到困惑。

在此处输入图片说明

FBCDRoutineViewController FBCDRoutineViewController

   - (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Routines"];
    self.routines = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

    [self.tableView reloadData];
}  

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"UpdateDevice"]) {
            NSManagedObject *selectedDevice = [self.routines objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
            FBCDRoutineViewController *destViewController = segue.destinationViewController;
            destViewController.routines = selectedDevice;
        }

FBCDRoutineDetailViewController FBCDRoutineDetailViewController

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"RoutinesDetails"];
    self.routines = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

}

- (void)viewDidLoad
{
    [[self navigationController] setNavigationBarHidden:NO animated:YES];

    [super viewDidLoad];
    // Do any additional setup after loading the view.
   if (self.routines) {
     [self.testLabel setText:[self.routines valueForKey:@"routinename"]];
   } 

}

FBCDRoutineDetailViewController FBCDRoutineDetailViewController

@property (strong) NSManagedObject *routines;

This is my first time with core data and I am looking at how to show the Details entity. 这是我第一次使用核心数据,我正在研究如何显示Details实体。 Am I Close to getting it working? 我快要开始运作了吗? If not can I get directed at to what I should be looking at. 如果没有,我可以直接去看我应该看的东西。

Any suggestions? 有什么建议么?

I think I see several problems--mostly related to the timing of these various calls. 我认为我看到了几个问题-主要与这些电话的时间安排有关。

I believe viewDidLoad is called on your detail view before the prepareForSegue is called. 我相信在调用prepareForSegue之前,会在详细视图上调用viewDidLoad。 So your code in viewDidLoad is trying to display data about your detail item before it has been set. 因此,您的viewDidLoad中的代码试图在设置详细信息之前显示有关您的详细信息的数据。

Then the code in viewDidAppear looks to be overwriting the value you set in prepareForSegue, which doesn't make sense to me (although by this time the view is already displayed and it's not going to affect the label you tried to set in viewDidLoad). 然后,viewDidAppear中的代码似乎正在覆盖您在prepareForSegue中设置的值,这对我来说没有任何意义(尽管此时该视图已经显示,并且不会影响您试图在viewDidLoad中设置的标签)。

Also, executeFetchRequest: returns an NSArray, not an NSManagedObject, so assigning the result of your fetch to your NSArray property is a BAD idea. 另外,executeFetchRequest:返回一个NSArray,而不是NSManagedObject,因此将获取结果分配给NSArray属性是一个不好的主意。

If I understand your problem correctly, you want to display all RoutinesDetails objects that are related to the Routines object passed in prepareForSegue . 如果我正确理解了您的问题,则希望显示与prepareForSegue传递的Routines对象相关的所有RoutinesDetails对象。

Then you would declare two properties in the FBCDRoutineDetailViewController : 然后,您将在FBCDRoutineDetailViewController声明两个属性:

@property (strong) NSManagedObject *routines; // the passed object
@property (strong) NSManagedObject *routineDetails; // the displayed details objects

and fetch them like this: 并像这样获取它们:

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"RoutinesDetails"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"routineinfo == %@", self.routines];
[fetchRequest setPredicate:predicate];
NSError *error;
self.routineDetails = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

self.routineDetails is now the data source array for the details view. self.routineDetails现在是详细信息视图的数据源数组。

(Remark: For displaying the result set of a Core Data request in a table view, you might also consider to use a NSFetchedResultsController .) (备注:为了在表视图中显示Core Data请求的结果集,您可能还考虑使用NSFetchedResultsController 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM