-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSManagedObjectContext_PDCategory.m
91 lines (70 loc) · 2.56 KB
/
NSManagedObjectContext_PDCategory.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// NSManagedObjectContext_PDCategory.m
// SproutedUtilities
//
// Created by Philip Dow on 5/14/07.
// Copyright Sprouted. All rights reserved.
// All inquiries should be directed to [email protected]
//
#import <SproutedUtilities/NSManagedObjectContext_PDCategory.h>
@implementation NSManagedObjectContext (PDCategory)
- (NSManagedObject *)managedObjectForURIRepresentation:(NSURL *)aURL
{
NSManagedObject *theObject = nil;
NSManagedObjectID *objectID = [[self persistentStoreCoordinator] managedObjectIDForURIRepresentation:aURL];
if ( objectID == nil )
NSLog(@"%@ %s - no object with uri represenation %@", [self className], _cmd, aURL);
else
theObject = [self objectWithID:objectID];
return theObject;
}
- (NSManagedObject*) managedObjectRegisteredForURIRepresentation:(NSURL*)aURL
{
NSManagedObject *theObject = nil;
NSManagedObjectID *objectID = [[self persistentStoreCoordinator] managedObjectIDForURIRepresentation:aURL];
if ( objectID == nil )
NSLog(@"%@ %s - no object with uri represenation %@", [self className], _cmd, aURL);
else
theObject = [self objectRegisteredForID:objectID];
return theObject;
}
#pragma mark -
- (NSManagedObject*) managedObjectForUUIDRepresentation:(NSURL*)aURL
{
// the URL should be in the form of scheme://entityName/uuid
NSString *entityName = [aURL host];
NSString *uuid = [aURL path];
if ( [uuid characterAtIndex:0] == '/' )
uuid = [uuid substringFromIndex:1];
return [self managedObjectForUUID:uuid entity:entityName];
}
- (NSManagedObject*) managedObjectForUUID:(NSString*)uuid entity:(NSString*)entityName
{
// the managed objects of entity name must include a string "uuid" attribute
NSManagedObject *theObject = nil;
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entityName inManagedObjectContext:self];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"uuid == %@", uuid];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSError *error = nil;
NSArray *results;
[request setEntity:entityDescription];
[request setPredicate:predicate];
results = [self executeFetchRequest:request error:&error];
if ( results == nil )
{
NSLog(@"%@ %s - error executing fetch request, error: %@, request: %@", [self className], _cmd, error, request);
theObject = nil;
}
else
{
if ( [results count] == 1 )
theObject = [results objectAtIndex:0];
else
{
NSLog(@"%@ %s - the fetch did not return a single object but %i objects, %@", [self className], _cmd, [results count], request);
theObject = nil;
}
}
return theObject;
}
@end