简体   繁体   中英

iPhone core data: How to pass and fetch Entities between NIBS

I have core data entity: "mole," which I display in a TableView. When I select each in turn, I pass the selected mole to the next NIB using ...

controller.mole = [moleArray objectAtIndex:indexPath.row]; // pass the relevant mole to next NIB

When second NIB loads, I want to retrieve just the "details" for just the "mole" selected. I am using the following:

NSManagedObjectContext *context = [mole managedObjectContext]; // find details for the selected mole

NSFetchRequest *request = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Details" inManagedObjectContext:context];
[request setEntity:entity];

// now sort

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"detailsDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];

// Having created a fetch request, you now execute it. The events array needs to be mutable, so make a mutable copy of the result.

NSError *error = nil;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {

    NSLog(@"details mutableFetchResults = nil");

}

// The final steps are to set the view controller’s events array instance variable and to release objects that were allocated.

[self setDetailsArray:mutableFetchResults];
[mutableFetchResults release];
[request release];

Trouble is the "detailsArray" is returning the Details of all the Moles. I cannot seem to retrieve the Details for just the specific Mole selected. I presume setting "context" is incorrect. Help appreciated.

Does the Mole entity have a to-many relationship with Details ? If so, you can traverse that relationship and not have to execute a fetch request. Let's say you do have such a relationship and that it's called details . You can do this:

NSSet *details = [mole valueForKey:@"details"];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"detailsDate" ascending:NO];
NSArray *detailsArray = [details sortedArrayUsingSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
// [sortDescriptor release];  if you're not using ARC

The trick is that relationships are modeled as NSSet objects, so you'll still need to sort the value to get an ordered array.

Your problem is you're not filtering the search over your Details entity. You should make a NSPredicate with the mole id (or some kind of identifier) of the mole you passed to the next controller to filter that search and only get the mole you want

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"moleID == %@", mole.id];

Something like that, hope this helps.

If you have a Mole managed object already in hand, you don't need a fetch to find it's related Detail objects. Instead you just walk the relationship.

It sounds like you have a data model something like this:

Mole{
  //.. some attributes
  details<-->>Detail.mole
}

Detail{
  //... some attributes
  mole<<-->Mole.details
}

If you have a custom NSManagedObject subclass, then you would just use aMole.details to get a set of its related details:

NSSet *details=aMoleObj.details;

If your just using generic NSManagedObjects, then you would use:

NSSet *details=[aMoleObj valueForKey:@"details"];

Fetches are always for finding an object you don't have a relationship for. If you already have a related object in hand, you just query the relationship.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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