0

I would like to use entity objects but not store them... I read that I could create them like this:

myElement = (Element *)[NSEntityDescription insertNewObjectForEntityForName:@"Element" inManagedObjectContext:managedObjectContext];

And right after that remove them:

[managedObjectContext deleteObject:myElement];

then I can use my elements:

myElement.property1 = @"Hello";

This works pretty well even though I think this is probably not the most optimal way to do it...

Then I try to use it in my UITableView... the problem is that the object get released after the initialization. My table becomes empty when I move it!

Thanks

edit: I've also tried to copy the element ([myElement copy]) but I get an error...

paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
Nate
  • 7,606
  • 23
  • 72
  • 124

5 Answers5

0

One option drawn from an answer to a similar question is initializing the NSManagedObject with a nil context:

Element *myElement = [[Element alloc] initWithEntity:entity
                      insertIntoManagedObjectContext:nil];

or

Element *myElement = [NSEntityDescription insertNewObjectForEntityForName:@"Element"
                                                   inManagedObjectContext:nil];
Community
  • 1
  • 1
paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
  • The latter one gives me an exception: Uncaught exception: +entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name – bompf May 20 '14 at 15:49
0

What I did was using an in-memory store. You can do it as described here: http://commandshift.co.uk/blog/2013/06/06/multiple-persistent-stores-in-core-data/

bompf
  • 1,374
  • 1
  • 18
  • 24
0

couldn't you just do

Element *myElement = [[Element alloc] init];

Then do with it whatever you want, presumably you will add it to an array so it says around for your UITableView.

jamone
  • 17,253
  • 17
  • 63
  • 98
  • I've already tried it... I get an error: Failed to call designated initializer on NSManagedObject class 'Element' - I don't really understand why it doesn't implement the NSObject initializer! – Nate Apr 22 '10 at 19:37
0

Maybe you can try to have two store coordinators in your project. One which have persistence, and the other with no persistence.

Shai Mishali
  • 9,224
  • 4
  • 56
  • 83
Quentin
  • 1,741
  • 2
  • 18
  • 29
  • the problem is that I need to copy part of some persistent objects on the temporary ones! I get an error when the managed object context are not the same! – Nate Apr 26 '10 at 15:06
  • So in this case, I think I would have used dictionary to store data, in order to create managed object with the dictionary if object should be persisted. But it's hard to help you without knowing your project architecture. Regards, – Quentin Apr 28 '10 at 07:15
  • OK... I created another class which is initializable! It's not very clean but it works. Thanks for your help. – Nate Apr 28 '10 at 09:57
0

Consider using transient objects --- that are handled by the managed object context like any other object, but not written to disk on a save operation. They are typically employed to model runtime-only objects, as I suspect you are trying to do.

Here is some info on them: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdNSAttributes.html

http://2pi.dk/tech/cocoa/transient_properties.html

NSSplendid
  • 1,957
  • 1
  • 13
  • 14