Re: NSManagedObjectContext -insertObject: Cancels Prior Deletion -- BUT!
Re: NSManagedObjectContext -insertObject: Cancels Prior Deletion -- BUT!
- Subject: Re: NSManagedObjectContext -insertObject: Cancels Prior Deletion -- BUT!
- From: Jerry Krinock <email@hidden>
- Date: Tue, 5 May 2009 14:32:53 -0700
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@hidden)
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:
This email sent to email@hidden