• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSManagedObjectContext -insertObject: Cancels Prior Deletion -- BUT!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSManagedObjectContext -insertObject: Cancels Prior Deletion -- BUT!


  • Subject: Re: NSManagedObjectContext -insertObject: Cancels Prior Deletion -- BUT!
  • From: Alexander Spohr <email@hidden>
  • Date: Wed, 6 May 2009 11:13:39 +0200

* 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@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

_______________________________________________

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


  • Follow-Ups:
    • Re: NSManagedObjectContext -insertObject: Cancels Prior Deletion -- BUT!
      • From: Jerry Krinock <email@hidden>
References: 
 >NSManagedObjectContext -insertObject: Cancels Prior Deletion? (From: Jerry Krinock <email@hidden>)
 >Re: NSManagedObjectContext -insertObject: Cancels Prior Deletion? (From: Dave Fernandes <email@hidden>)
 >Re: NSManagedObjectContext -insertObject: Cancels Prior Deletion -- BUT! (From: Jerry Krinock <email@hidden>)

  • Prev by Date: Re: How to align a custom view in a toolbar ???
  • Next by Date: Re: UITextField not sending messages to delegate
  • Previous by thread: Re: NSManagedObjectContext -insertObject: Cancels Prior Deletion -- BUT!
  • Next by thread: Re: NSManagedObjectContext -insertObject: Cancels Prior Deletion -- BUT!
  • Index(es):
    • Date
    • Thread