I have a question regarding editing/deleting Core Data objects in a UIListView
that are related to another object.
To simplify the problem, let's say I have a list view of Family
objects. Tapping on a Family name opens a list view of FamilyMember
objects for the chosen Family
object. I have a one to many relationship between Family
and FamilyMember
set up in Core Data.
I perform an NSFetchRequest
to grab the list of all 'Family' objects in my initial table view controller and put them into an NSFetchedResultsController
. When a family in the list is tapped, that 'Family' object is passed into the FamilyMembersViewController
with prepareForSegue
.
The issue I'm facing is that since I passed in a 'Family' object directly from the initial view controller to FamilyMembersViewController
, I never had to create a FetchRequest or a NSFetchedResultsController
in FamilyMembersViewController
. Therefore, some editing functions that worked great on the initial view now don't work in the FamilyMembersViewController
.
How do I take advantage of NSFetchedResultsController
functions (such as the following) in a view that doesn't have a NSFetchedResultsController
? Do I need to re-query Core Data, limiting the results to a specific 'Family" object? It seems like a waste since I already have the object available in my view.
Am I approaching this functionality incorrectly? I guess I'm just hoping to have the functions be similar from one view controller to the next.
I've tried making a new array called 'familyMembers' and a corresponding NSFetchedResultsController
within my FamilyMembersViewController
and predicated it with this:
var predicate = NSPredicate(format: "family == %@", "\(self.family)")
where self.family
is the 'Family' object passed from the initialView
. But in this case, for some reason the "self.family" part just returns an empty string, when really it should return the Family object. Even if it did return the Family object, it doesn't seem like passing that into the predicate would help anything, since the predicate seems to match only strings. I can't quite figure out how to format a Predicate with a block statement.
Any help would be much appreciated! Thank you for reading.
func controllerWillChangeContent(controller: NSFetchedResultsController!) {
tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!, atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!) {
switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
case .Update:
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
default:
tableView.reloadData()
}
families = controller.fetchedObjects as [Family]
}
func controllerDidChangeContent(controller: NSFetchedResultsController!) {
tableView.endUpdates()
}
尽管谓词参数不必是字符串,但您的做法正确无误-您可以传递family对象本身:
var predicate = NSPredicate(format: "family == %@", self.family)
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.