Domanda

I'm using this demo core data iOS app to help build my own because it implements drag and drop functionality in the table view cells. The app seems to have been created solely to demonstrate how to implement drag and drop. However, the app does several things that I haven't seen done in any of the core data tutorials I used to get started with the api. In the data model (see image) it sets up a parent child relationship within a single entity (Event) enter image description here.

In the MasterViewController it also sets up a rootObject

@property (strong, nonatomic) NSManagedObject *rootObject;

and then, when it sets its fetch predicate, it refers to "subItems" on the rootObject and, as you'll see from the image, the subItems are in the parent/child relationship in the entity

- (NSPredicate *) fetchPredicate
{

    NSMutableSet* relationship = [self.rootObject mutableSetValueForKey:@"subItems"];
    return [NSPredicate predicateWithFormat:@"self in %@",relationship];
}

Another important piece of code might be in the insert new object method, when he checks whether the root object exists. Note, the log statement runs everytime I add a new object to the tableView

if (self.rootObject) {
    NSLog(@"rootObject exists");

    NSMutableSet* relationship = [self.rootObject mutableSetValueForKey:@"subItems"];

    NSManagedObject *lastObject = [self.fetchedResultsController.fetchedObjects lastObject];
    double lastObjectDisplayOrder = [[lastObject valueForKey:@"displayOrder"] doubleValue];
    [newManagedObject setValue:[NSNumber numberWithDouble:lastObjectDisplayOrder + 1.0] forKey:@"displayOrder"];

    [relationship addObject:newManagedObject];
}

[self saveContext];

Can you explain

a) what is happening/why is it necessary/what is the purpose of setting up that relationship within the entity?

b) assuming it's related, why might the author declare an NSManagedObject as a rootObject? None of the core data demos I've seen actually declare NSManagedObjects. For example, the master/detail template in xCode just declares, which gives you a basic core data project

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
È stato utile?

Soluzione

what is happening/why is it necessary/what is the purpose of setting up that relationship within the entity?

It isn't necessary, it's just an example and an easy way to create a hierarchy for a master-detail interface with a minimal Core Data model

assuming it's related, why might the author declare an NSManagedObject as a rootObject?

It's a simple way to create a 'master' object. Usually the 'master' or 'root' object would be defined by the only object in the context whose parent == nil.

None of the core data demos I've seen actually declare NSManagedObjects.

That doesn't really mean anything. Apps will often temporarily hold a pointer to a managed object instance. Indeed, a fetched request controller can hold multiple pointers to objects at any one time.

For example, the master/detail template in xCode just declares, which gives you a basic core data project

Apple examples aren't the best. That template also has the app delegate owning the Core Data stack and thats a terrible decision...

Aside:

The predicate can use the relationship better by using the relationship 'backwards'. i.e. rather than load the children relationship and check the contents it should reference the parent and check for equality:

@"parent == %@", selectedParent
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top