1

I have multiple UITextView instances in my UIViewController and would like to save the typed-in text in Core Data when the view controller's view disappears or a specific button is clicked. Ideally, I would use the textViewDidEndEditing delegate method to save the changes to Core Data but the problem is that if a user changes a text view and then hits the said button, this delegate method is not called. So how should I save the changes?

One option I can think of is to leverage textViewDidChange - but that method is called each time a character is typed in. If I were to save the text to Core Data within this method, wouldn't that be inefficient?

Are there other ways you can recommend for me to achieve this?

Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
vikram17000
  • 453
  • 5
  • 18
  • Why would the view disappear without the button being tapped? Most likely you should be tying the save action to the button tap. – Mark Leonard Jun 28 '16 at 10:42

2 Answers2

0

It's unclear exactly what the relationship is between button pushing and saving data, but one way would be to leverage viewWillDisappear: and make your Core Data insertions fire once you know the text inputs are final and the user is leaving.

You could fire the same insertion method when the button is tapped.

sakurasunris3
  • 579
  • 5
  • 9
0

You may have a local NSString variable to hold the updated value of your string and you can save it to CoreData when you finish your work with screen. It may be on : - A button touch - View disappearing - An error ocured in page

If you do not want to have a save action, you can also set a timer to count the time starting from the last edit moment of textViewDidChange and you can trigger timer if there is no change for the last 3-5 seconds.