automaticallyMergesChangesFromParent

Working with NSPersisentCoordinator?

    lazy public var viewContext: NSManagedObjectContext = {

        let viewContext = self.persistentContainer.viewContext

        viewContext.automaticallyMergesChangesFromParent = true

        return viewContext

    }()

 

I suggest setting automaticallyMergesChangesFromParent so it can observe changes from the parent store. It’s also a good prereq for using the new newBackgroundContext() function because instead of spawning a child from the View Context, it spawns a private MOC directly from the persistent store (so no parent!).

It took me a good while to realize that!

NSBatchDeleteRequest

Here’s an example:

let batchDeleteFetchRequest = NSFetchRequest(entityName: "Thing")
batchDeleteFetchRequest.sortDescriptors = [NSSortDescriptor(key: "uri", ascending: true)]
batchDeleteFetchRequest.predicate = NSPredicate(format: "NOT (uri IN %@)", URIsInDataDump)
let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: batchDeleteFetchRequest)
        
do {
    try managedObjectContext.executeRequest(batchDeleteRequest)
} catch {
    self.logger.error("-15")
}

 

There isn’t much official documentation out there because NSBatchDeleteRequest doesn’t exist in documentation, only a video clip from Core Data’s spotlight at WWDC.

Anyway, when running the above code and watching the context, you’ll notice no changes were acknowledged, though loading a new fetch does show the items were deleted. What?

Well, NSBatchDeleteRequest works by deleting directly from the store and even though it’s the managedObjectContext executing the request, it happens outside of its knowledge and therefore, isn’t aware of the persistent store change.

This means that iCloud Core Data won’t update, and even trivial things like NSFetchedResultsController won’t fire off changes. One interaction I’m unsure of is how it interacts with parent contexts – so stay tuned for a little test into that later!

The Dalai Lama’s 18 Rules For Living

  1. Take into account that great love and great achievements involve great risk.
  2. When you lose, don’t lose the lesson.
  3. Follow the three Rs:
    • Respect for self
    • Respect for others
    • Responsibility for all your actions.
  4. Remember that not getting what you want is sometimes a wonderful stroke of luck.
  5. Learn the rules so you know how to break them properly.
  6. Don’t let a little dispute injure a great friendship.
  7. When you realize you’ve made a mistake, take immediate steps to correct it.
  8. Spend some time alone every day.
  9. Open your arms to change, but don’t let go of your values.
  10. Remember that silence is sometimes the best answer.
  11. Live a good, honourable life. Then when you get older and think back, you’ll be able to enjoy it a second time.
  12. A loving atmosphere in your home is the foundation for your life.
  13. In disagreements with loved ones, deal only with the current situation. Don’t bring up the past.
  14. Share your knowledge. It’s a way to achieve immortality.
  15. Be gentle with the earth.
  16. Once a year, go someplace you’ve never been before.
  17. Remember that the best relationship is one in which your love for each other exceeds your need for each other.
  18. Judge your success by what you had to give up in order to get it.