3

For a simple note application, there is a table view controller with a fetchedResultsController that has a list of saved notes in core data. The user can either click the + button to add a new note or click on one of the records in the table view to edit an existing note. In either case, a modal view controller with a text view shows up.

I want to save as the user types each letter, so in textViewDidChange I am updating Core Data. If the user types too fast, the core data saves hang. I recently added code to add the updates to NSOperationQueue which was an idea taken from here:
How to implement work queue in iOS where only newest request is handled? and that has helped somewhat.

Here's the problem I need help with. Let's say the user types an update in the textView, then pushed Done to return to the table view controller, and then clicks on the same note to edit the note again before the original update was refreshed in the table view. The user is presented with a textView that has the old data. The update is not there. If the user goes back to the table view and edits the note again, the latest updates show.

What are some approaches to consider, or how could I attempt to fix this issue? I want the textView to always show the latest updates even if user is switching back and forth from the textView and tableViewController faster than a user should ;)

Community
  • 1
  • 1
dave
  • 33
  • 3

2 Answers2

2

You don't need to save the context everytime the user adds a character, do it only when he finishes adding the note, or at least when the textfield loses its focus (first responder resigns). There's really no point in doing it everytime he taps. Saving the context is a pretty expensive operation.

Rad'Val
  • 8,895
  • 9
  • 62
  • 92
  • Thanks for the ideas. Originally I was saving only after the user is done updating the note, however I would like to protect the user from losing any unsaved updates such as when the battery dies while the user is the middle of typing a note. Besides saving after every letter is added, is there another way to ensure that the note is saved when a device loses power? – dave Dec 02 '12 at 05:26
  • any event like that will call either applicationWillResign: or applicationWillTerminate: from the apps delegate. Adding a save there should solve your problem. (in the battery case, applicationWillTerminate: is called by the way, as long as your app is in foreground, but since an user can write a note only while the app is in foreground, I guess that suits your needs) – Rad'Val Dec 02 '12 at 05:42
0

Maybe try saving all of the changes at once in:

- (void)viewWillDisappear:(BOOL)animated

Instead of constantly saving as the user types, which is a bit redundant.

If the user goes to their home screen, then save the changes in

- (void)applicationDidEnterBackground
klcjr89
  • 5,862
  • 10
  • 58
  • 91