1

I am creating an NSManagedObject subclass (Group), set some properties/attributes and then pass it on to a different object (where it is stored in a strong property). This object takes some of the Group information and sends it to a server over an instance of GCDAsyncSocket. The server responds with some information that I then want to store as an attribute of the Group. However, by the time the server responds back and GCDAsyncSocket's delegate is called, all of the Group's attributes are set to nil.

Since I'm using UIManagedDocument for my Core Data implementation, when the auto-save kicks in, I get the following error:

Error Domain=NSCocoaErrorDomain Code=134030 "The operation couldn’t be completed. (Cocoa error 134030.)" UserInfo=0x1f5c96f0 {NSAffectedObjectsErrorKey=( "<Group: 0x1f5aa300> (entity: Group; id: 0x1f5c73b0 ; data: {<entities here, nil values>})" ), NSUnderlyingException=Cannot update object that was never inserted.}

However, I know that the objects are inserted. It did some research and found a lot of problems that were related to using two or more different managed object contexts, but that is not my problem (as the only managed object context I ever get is from the UIManagedDocument).

Some code:

@property(nonatomic, strong) Group *currentGroup;

// ....

- (void)storeGroupOnServer:(Group *)group {
    self.currentGroup = group;
    NSLog("%@", self.currentGroup); // correct value for name attribute
    [self.currentGroup addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL];

    // do some other things, unrelated to this problem
    // write data to socket
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    // This method is called before the socket returns with response data

    NSLog(@"%@", (Group *)object.name); // incorrect value for name attribute (nil)
    NSLog(@"%@", self.currentGroup); // same as above
}

Anybody who has a clue what I'm doing wrong here?

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
  • What is the difference between `self.group` and `self.currentGroup`? Is your context being reset during the fetch? on what thread `self.group.name` is set? – Dan Shelly Apr 27 '13 at 14:30
  • I'm sorry, `self.group` should have been `self.currentGroup`, fixed now. The context is not being reset as far as I can tell (memory location remains the same throughout). I thought it was a threading issue, but looking at the stack traces when those calls are made, they all show up under "Thread 1". – Scott Berrevoets Apr 27 '13 at 16:35
  • Did you set a breakpoint on setCurrentGroup: to see if it is getting modified unexpectedly? Can you compare the objectID of the managed object, before and after, to ensure it is the same object? – bneely Sep 24 '13 at 09:36

0 Answers0