Database Reference
In-Depth Information
Joe asks:
Can We Pass the NSManagedObjectContext by
Reference?
Although this is feasible, it's not recommended. When we pass objects by reference,
a proxy object is created on the receiver that sends all messages back to the server
to be performed. This is fine for objects with low complexity, but when we're dealing
with highly complex objects, such as the NSManagedObjectContext , performance suffers.
During experimentation, I received some very unusual errors deep within the Core
Data API when the NSManagedObjectContext was passed by reference. It's probably best
to avoid this approach.
-deleteObject Implementation
DistributedCDServer/AppDelegate.m
- (oneway void )deleteObject:(byref NSManagedObject*)object;
{
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *local = [context objectWithID:[object objectID]];
if ([local isDeleted]) {
return ;
}
if (![local isInserted]) {
[self saveAction:self];
}
[context deleteObject:local];
}
The -deleteObject is similar to the -createObject discussed earlier. However, in this
method, we need to retrieve a local reference to a passed-in NSManagedObject .
If we attempt to delete the referenced NSManagedObject directly, the NSManagedOb-
jectContext implodes deep within the API. No doubt this is caused by the double
proxy of looping to the remote and then back again to the server. To solve
this issue, we retrieve the NSManagedObjectID from the referenced NSManagedObject
and use it to retrieve a local reference to the NSManagedObject via the -objectWithID:
of the NSManagedObjectContext . Once we have a local reference to the NSManagedOb-
ject , we check to see whether it is freshly inserted or already deleted. If it is
freshly inserted, we need to persist it before we can delete it. Therefore, we
save the NSManagedObjectContext and then delete the NSManagedObject . If the
NSManagedObject has already been deleted, we return to the caller.
 
 
 
Search WWH ::




Custom Search