2

I am new to Core Data and I feel that I don't get a hang of it. When you are going to create a new managed object you have to use the method that I pasted in at the bottom from the NSEntityDescription class.

Now is this object registered in the context, it is right? Why do you have the insertObject: in NSManagedContext then? It works without invoking that method after using the one in NSEntityDescription. Could somebody explain?

NSManagedContext

insertObject:

NSEntityDescription

+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context
LuckyLuke
  • 47,771
  • 85
  • 270
  • 434

1 Answers1

3

You can create NSManagedObject with nil context and then decide whether you want to add it to context or not. You may also like this: Is there a way to instantiate a NSManagedObject without inserting it?

Community
  • 1
  • 1
Adam
  • 26,549
  • 8
  • 62
  • 79
  • Ah, so if you create the instance with nil as context, then you later pass it to the insertObject? – LuckyLuke May 24 '12 at 07:15
  • But if that is not the intended use as some of them points out, why do Apple provide the method? Or is passing `nil` as a context normal? – LuckyLuke May 24 '12 at 07:18
  • I use it all the time without any problems. The method may be useful when you have more than one context. Then you can decide which one to save. – Adam May 24 '12 at 07:27
  • Okey, but if you use the NSEntityDescription method you already have registered the returned object with a context and you don't invoke the insertObject: in NSManagedContext right? – LuckyLuke May 24 '12 at 07:28
  • Yes, exactly. Only use insertObject: when the context is nil. – Adam May 24 '12 at 07:31