开发者

What's the best way to log Core Data NSManagedObject IDs on creation?

开发者 https://www.devze.com 2023-04-07 02:12 出处:网络
I\'ve dealt with a couple errors recently in which I will get an exception that mentions a managed object开发者_如何学Python by URI. The URI is somewhat helpful. It looks kind of like x-coredata://bla

I've dealt with a couple errors recently in which I will get an exception that mentions a managed object开发者_如何学Python by URI. The URI is somewhat helpful. It looks kind of like x-coredata://blahblahblahblahblah/EntityName/p22. At least I can tell what type of entity the problem occurred on, but I have not as yet been able to track it down to a particular object. So far I've been able to debug the problems without figuring that out.

I imagine I could track it down. I could probably open up the database and do SQL queries until I found something that matched up to something in the URL. However that seems like kind of a lot of trouble, and also not very efficient if I have to debug a problem that happens on a beta tester's or deployed user's device. So I think it would be nice to log the object ID URI when the object is created, along with some properties I can recognize the object by.

Should be simple, right? Just right after I create an object in my code I do an NSLog something like

NSLog(@"Created Foo instance: %@", [foo.objectID URIRepresentation]);

Only problem is, the URIs I;m getting don't look like the above. They look more like x-coredata:///EntityName/blahblahblahblahblah. I realized I'm probably getting temporary IDs.

So, how can I match that up with the permanent ID? If I could find out a hook where I can just put a log message saying "Object with temporary ID %@ reassigned permanent ID %@" that would be all I need.


I question the value of this but it is possible via two methods in the NSManagedObject itself.

First, set up a transient NSString either directly in the model or in the subclass. Then override the following methods:

- (void)willSave
{
  if (![[self objectID] isTemporaryID]) return;
  [self setTemporaryIDString:[[[self objectID] URIRepresentation] absoluteString]];
}

- (void)didSave
{
  if (![self temporaryIDString]) return;

  NSLog(@"%@ moved to %@", [self temporaryIDString], [[[self objectID] URIRepresentation] absoluteString]);
  [self setTemporaryIDString:nil];
}


Core Data NSManagedObject has a property debugDescription, use it.

NSManagedObject * customer = nil;

NSLog(@"customer : %@", customer.debugDescription);
0

精彩评论

暂无评论...
验证码 换一张
取 消