TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.