* now entering guessing-land *
The MOC knows the objects is deleted. If you try to refetch it, the
MOC has to clear all attributes because it does not exist anymore (in
the MOCs view of the database).
Is the primary key still the same? If it is not null now as well...
* exit guessing-land *
You should not re-fetch a dirty MOC at any case. Save its changes first.
Sure, your object will then be lost. But a deletion should exactly do
that. To revive an object from the dead is a strange concept (just in
my opinion).
Try to either rearrange your delete/insert to just rehang the object
into another parent, create a new object (you should not depend on the
primary key anyway) or _maybe_ you could use a second MOC to fetch and
insert. The latter depends on your projects structure. In any case
using multiple MOCs for different jobs is a good idea.
Hope that helps,
atze
Am 05.05.2009 um 23:32 schrieb Jerry Krinock:
Thanks for the answers, "yes" and "no", but I've found a problem
doing so....
1. Insert a managed object.
2. Set an attribute in the object.
3. Delete the object from its moc.
4. Execute a fetch request in the moc with no predicate.
5. Get the attribute from the object.
Expected Result: The attribute set in step 2
Actual Result: nil
Why does the attribute disappear? Continuing, the attribute does
not come back after re-inserting the object. Also, I've tried a -
processPendingChanges before deleting, but that does not help.
Here are the steps written in code:
// Insert a Foo and name it "Murphy"
NSManagedObject* foo = [NSEntityDescription
insertNewObjectForEntityForName:@"Foo"
inManagedObjectContext:moc] ;
[foo setValue:@"Murphy"
forKey:@"name"] ;
// Delete the Foo from the moc
[moc deleteObject:foo] ;
// Create a fetch request for all objects.
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Foo"
inManagedObjectContext:moc]];
// Execute the fetch request
// (Note: We ignore the fetch result.)
NSLog(@"1000 name of foo: %@", [foo valueForKey:@"name"]) ;
[moc executeFetchRequest:fetchRequest
error:NULL] ;
NSLog(@"2000 name of foo: %@", [foo valueForKey:@"name"]) ;
// Re-insert the Foo into the moc
[moc insertObject:foo] ;
NSLog(@"3000 name of foo: %@", [foo valueForKey:@"name"]) ;
Console Output:
2009-05-05 14:25:43.533 CoreDataUtility[8186:10b] 1000 name of foo:
Murphy
2009-05-05 14:25:43.537 CoreDataUtility[8186:10b] 2000 name of foo:
(null)
2009-05-05 14:25:43.539 CoreDataUtility[8186:10b] 3000 name of foo:
(null)
In case anyone would like to run the code, here's the whole project
in one file. It's based on Apple's Core Data Utility Sample Project:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
// Note: This method returns a retained, not autoreleased, instance.
NSManagedObjectModel *getStaticManagedObjectModel() {
static NSManagedObjectModel *mom = nil;
if (mom != nil) {
return mom;
}
mom = [[NSManagedObjectModel alloc] init];
NSEntityDescription *fooEntity = [[NSEntityDescription alloc]
init];
[fooEntity setName:@"Foo"];
[fooEntity setManagedObjectClassName:@"Foo"];
[mom setEntities:[NSArray arrayWithObject:fooEntity]];
NSAttributeDescription *nameAttribute;
nameAttribute = [[NSAttributeDescription alloc] init];
[nameAttribute setName:@"name"];
[nameAttribute setAttributeType:NSStringAttributeType];
[nameAttribute setOptional:YES];
[fooEntity setProperties:[NSArray arrayWithObject: nameAttribute]];
[fooEntity release] ;
return mom;
}
// Note: This method returns a retained, not autoreleased, instance.
NSManagedObjectContext *getStaticManagedObjectContext() {
static NSManagedObjectContext *moc = nil;
if (moc != nil) {
return moc;
}
moc = [[NSManagedObjectContext alloc] init];
NSPersistentStoreCoordinator *coordinator =
[[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel: getStaticManagedObjectModel()];
[moc setPersistentStoreCoordinator: coordinator];
[coordinator release] ;
NSError *error;
NSPersistentStore *newStore ;
newStore = [coordinator
addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil
URL:nil
options:nil
error:&error];
if (newStore == nil) {
NSLog(@"Store Configuration Failure\n%@",
([error localizedDescription] != nil) ?
[error localizedDescription] : @"Unknown Error");
}
return moc;
}
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Create Core Data stack
NSManagedObjectModel *mom = getStaticManagedObjectModel();
NSManagedObjectContext *moc = getStaticManagedObjectContext();
// Insert a Foo and name it "Murphy"
NSManagedObject* foo = [NSEntityDescription
insertNewObjectForEntityForName:@"Foo"
inManagedObjectContext:moc] ;
[foo setValue:@"Murphy"
forKey:@"name"] ;
// Delete the Foo from the moc
[moc deleteObject:foo] ;
// Create a fetch request for all objects.
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Foo"
inManagedObjectContext:moc]];
// Execute the fetch request
// (Note: We ignore the fetch result.)
NSLog(@"1000 name of foo: %@", [foo valueForKey:@"name"]) ;
[moc executeFetchRequest:fetchRequest
error:NULL] ;
NSLog(@"2000 name of foo: %@", [foo valueForKey:@"name"]) ;
// Re-insert the Foo into the moc
[moc insertObject:foo] ;
NSLog(@"3000 name of foo: %@", [foo valueForKey:@"name"]) ;
[moc release] ;
[mom release] ;
[pool release] ;
return 0;
}
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/atze%40freeport.de
This email sent to [email protected]
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]