UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

I can't understand the uncomment lines in Project 11, part 3

Forums > 100 Days of SwiftUI

In this page: https://www.hackingwithswift.com/books/ios-swiftui/using-an-alert-to-pop-a-navigationlink-programmatically

For the following code:

func deleteBook() {
    moc.delete(book)

    // uncomment this line if you want to make the deletion permanent
    // try? self.moc.save()
    presentationMode.wrappedValue.dismiss()
}

I can't understand when should we use the line "try? self.moc.save()". I think the "moc.delete(book)" has been made the deletion permanent.

3      

Yes but it saved in the CoreData so it will not delete from there until you save the change try? self.moc.save()

3      

Thanks for your help, but I still can't understand.

Based on my experiment:

  1. using "moc.delete(book)" without "try? self.moc.save()"
  2. open the app and insert a line of data
  3. close the app
  4. reopen the app (see the line there)
  5. get into the detail page of the line
  6. using the trash button to delete it.
  7. close the app
  8. reopen the app (can't see the line there)

Thus, there is no difference between using "try? self.moc.save()" or not.

Would you give me an example to let the "try? self.moc.save()" making a different result?

3      

Yeah you are right, today was working on this same project, but didn't mention this. Now I've tried and it really removes from the Core Data after moc.delete(book). Very interesting why so, as in docs it's mentioned that changes must be committed in order to completely remove.

3      

If you leave it comment when deleteBook() then stop the app and run again the book is still there, however if you uncomment then delete book and stop app then run again it not there.

Check out short video

https://twitter.com/nigelgee/status/1239998080558170113?s=21

4      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.