Database Reference
In-Depth Information
//Reload table view
tableView . reloadData ()
}
As you can see, the Core Data version of this method is much more complicated.
First, you have to create a new Walk entity and set its date attribute to now. Then,
you have to insert this walk into the currently selected dog's list of walks.
However, the walks attribute is of type NSOrderedSet . NSOrderedSet is immutable, so
you first have to create a mutable copy ( NSMutableOrderedSet ), insert the new walk
and then reset an immutable copy of this mutable ordered set back on the dog.
Note: Is adding a new object into a to-many relationship making your head
spin? Many people can sympathize, which is why many of the Core Data open
source projects have convenience methods to do this.
Core Data can make things easier for you, though. If the relationship weren't
ordered, you'd just be able to set the “one” side of the relationship (e.g.,
walk.dog = currentDog ) rather than the “many” side and Core Data would use
the inverse relationship defined in the model editor to add the walk to the
dog's set of walks.
Finally, you commit your changes to the persistent store by calling save on the
managed object context, and you reload the table view.
Build and run the app, and tap the plus (+) button a few times:
Great! The list of walks should now be saved in Core Data. Verify this by
terminating the app in the fast app switcher and re-launching from scratch.
Search WWH ::




Custom Search