2

iPhone forms, where do you put your validation?

People suggested me to directly put the value of my textfields in my core data entity, which I did (see my second edit). Thing is, when a person update an entity, since I put directly the value that the user is typing in my entity, even when I don't save my ManagedObjectContext, my TableViewController (using NSFRC) updates with the latest infos the user entered (even if he pressed cancel). If I restart the app, since it didnt save, everything is back to normal.

How can I avoid this?

Community
  • 1
  • 1
allaire
  • 5,995
  • 3
  • 41
  • 56
  • I'm confused of what your question is, are you trying to rollback changes instead of restarting the app, or do you want to update the managedObject after a textfield is changed? – Gobot Mar 22 '12 at 01:29
  • What I'm trying to fix is the fact that when a person click the cancel button or simply go back in the navigationcontroller, the data he entered WITHOUT pressing save don't update my TableViewController (that gets updated because I validate directly in my entity and somehow NSFRC updates by himself) – allaire Mar 22 '12 at 01:32

1 Answers1

5

I'm assuming you are using the same NSManagedObjectContext for both viewControllers. You could use a separate NSManagedObjectContext for each viewController. If you are using iOS 5.1 set the 1st viewController's managedObjectContext (MOC) as the parent of the 2nd viewControllers managedObjectContext. So if you save the 2nd view's MOC via [theContext save], it will merge the changes with the 1st MOC automagically. If you don't want to keep the changes simply pop that view off the navigation stack.

MOC's are scratchpads. So in essence you want to use that 2nd viewController as a scratch pad, until you hit save.

Gobot
  • 2,464
  • 1
  • 21
  • 26
  • 2
    You have to subscribe (1st viewController) to notifications of 2nd MOC. Then implement the `mergeChangesFromContextDidSaveNotification:` method. Apple made it way easier (less code) with 5.1. Look at `NSManagedObjectContext` apple docs, specifically their notifications. There are a bunch of examples of the notification method on this site since that was one of the main ways the last few years. – Gobot Mar 22 '12 at 01:50
  • Hmmm, is this really the way to go? Is it what you do to validate that a couple fields are simply not empty? Note that I'm using MagicalRecord. – allaire Mar 22 '12 at 01:53
  • Well validating and not persisting changes are different things. Why even save, validate before saving the MOC. – Gobot Mar 22 '12 at 01:55
  • What do you use to validate fields then? people suggested to create an entity and use validateForInsert: and validateForUpdate: here: http://stackoverflow.com/questions/9806551/iphone-forms-where-do-you-put-your-validation/9809222#comment12494236_9809222 – allaire Mar 22 '12 at 01:57
  • You can do that, that's one way. But remember, you are "inserting" into the MOC. So if you don't want the 1st MOC to be touched, you need to insert" into the 2nd which needs to be a separate MOC. – Gobot Mar 22 '12 at 01:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9168/discussion-between-allaire-and-gobot) – allaire Mar 22 '12 at 02:02
  • Hey Gobot, should I use NSPrivateQueueConcurrencyType or NSMainQueueConcurrencyType when I create my child MOC? Also, normal that NSConfinementConcurrencyType doesn't save? I saw this post to understand a bit better http://stackoverflow.com/questions/9657760/ios5-nsmanagedobjectcontext-concurrency-types-and-how-are-they-used butI'm still a bit confused :/ – allaire Mar 22 '12 at 19:32
  • I'm still wrapping my head around the differences too, although they do make much more sense to me at this point, especially when you start dealing with threads/background processes. In regards to parent/child MOCs, `NSPrivateQueueConcurrencyType` and `NSMainQueueConcurrencyType` only support parent child contexts. NSConfinementConcurrencyType needs to subscribe to MOC notifications. Look at the WWDC 2011 video on Core Data. They talk about this stuff and more. – Gobot Mar 23 '12 at 00:04