views:

70

answers:

4

Hi everyone,

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...

A: 

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
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!
ncohen
A: 

If you create a managed object without saving into the persistent store then you are misunderstanding managed object. You would better create an Array with values to populate to the table view instead of create the managed object.

A small question? Why you delete it immediately after the creation? How can that object be populated into the table view if you are trying to delete it immediately after you created it?

sfa
What I would like to do is to use the structure of my Element managedobject. These objects will be the content of my uitableview. By creating it and remove it from the context, the object Element will still be retained (then I can use it for my table) but won't be stored in the persistent storage!
ncohen
A: 

Hi,

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

Regards, Quentin

Quentin
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!
ncohen
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
OK... I created another class which is initializable! It's not very clean but it works. Thanks for your help.
ncohen
A: 

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
Yep but the problem is that some objects are transient and some aren't... how to handle these exceptions?
ncohen