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?