I got my solution to the silver challenge mostly satisfactorily done. I added an attribute. I installed a button next to the tags button to favorite a picture. I created a segue to another navigation controller/collection view where I could display my favorites by themselves. So far so good.
I then noticed if I restarted the app and it reloaded the persisted objects, that I had two copies of each favorited photo, one of which had the favorite attribute true and one of which had it false. At this point I haven’t been able to sort out how to get rid of the false one. Of course when I unfavorite a photo I wind up with three copies: two false and one true. The documentation on how to delete objects from the NSManagedObjectContext is hard to find.
I think I solved it by removing the existing photo, after creating the changed photo (with the favorite attribute changed). I also had to override viewWillAppear for PhotosViewController, because otherwise the program crashes when you attempt to select the deleted photo. The necessary language to delete an item is: context.delete(photo) before you save the context.
Strange, I was working on this tonight. Your implementation sounds very much like mine but so far I haven’t had any issues with duplicate photos, even after toggling the favorite attribute on & off multiple times on a couple of the photos.
I just toggled the value in the photo’s favorite attribute and then called persistentContainer.viewContext.save() on the photo store (wrapped in a do/catch, of course), and it seems to update the existing photo without creating a new one.
I am convinced the extra copy is there, because we have done nothing to remove it. When I went back to the main scene where under “viewDidLoad” we update the data source, the program does a “fetchAllPhotos” that goes back and gets the old copy. Now, before I change the favorite attribute I let foto = photo and do a context.delete(foto) before saving the context. That seems to solve it.