-
Notifications
You must be signed in to change notification settings - Fork 61
/
MySpotlightImporter.m
159 lines (120 loc) · 5.85 KB
/
MySpotlightImporter.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// MySpotlightImporter.m
// VisualJSONImporter
//
// Created by 2012 youknowone.org on 12. 6. 4..
// Copyright (c) 2012 youknowone.org. All rights reserved.
//
#import "MySpotlightImporter.h"
#define YOUR_STORE_TYPE NSXMLStoreType
@interface MySpotlightImporter ()
@property (nonatomic, strong) NSURL *modelURL;
@property (nonatomic, strong) NSURL *storeURL;
@end
@implementation MySpotlightImporter
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize modelURL = _modelURL;
@synthesize storeURL = _storeURL;
- (void)dealloc
{
[__managedObjectContext release];
[__persistentStoreCoordinator release];
[__managedObjectModel release];
[_modelURL release];
[_storeURL release];
[super dealloc];
}
- (BOOL)importFileAtPath:(NSString *)filePath attributes:(NSMutableDictionary *)spotlightData error:(NSError **)error
{
NSDictionary *pathInfo = [NSPersistentStoreCoordinator elementsDerivedFromExternalRecordURL:[NSURL fileURLWithPath:filePath]];
self.modelURL = [NSURL fileURLWithPath:[pathInfo valueForKey:NSModelPathKey]];
self.storeURL = [NSURL fileURLWithPath:[pathInfo valueForKey:NSStorePathKey]];
NSURL *objectURI = [pathInfo valueForKey:NSObjectURIKey];
NSManagedObjectID *oid = [[self persistentStoreCoordinator] managedObjectIDForURIRepresentation:objectURI];
if (!oid) {
NSLog(@"%@:%@ to find object id from path %@", [self class], NSStringFromSelector(_cmd), filePath);
return NO;
}
NSManagedObject *instance = [[self managedObjectContext] objectWithID:oid];
// how you process each instance will depend on the entity that the instance belongs to
if ([[[instance entity] name] isEqualToString:@"YOUR_ENTITY_NAME"]) {
// set the display name for Spotlight search result
NSString *yourDisplayString = [NSString stringWithFormat:@"YOUR_DISPLAY_STRING %@", [instance valueForKey:@"SOME_KEY"]];
[spotlightData setObject:yourDisplayString forKey:(NSString *)kMDItemDisplayName];
/*
Determine how you want to store the instance information in 'spotlightData' dictionary.
For each property, pick the key kMDItem... from MDItem.h that best fits its content.
If appropriate, aggregate the values of multiple properties before setting them in the dictionary.
For relationships, you may want to flatten values.
id YOUR_FIELD_VALUE = [instance valueForKey:ATTRIBUTE_NAME];
[spotlightData setObject:YOUR_FIELD_VALUE forKey:(NSString *) kMDItem...];
... more property values;
To determine if a property should be indexed, call isIndexedBySpotlight
*/
}
return YES;
}
static NSURL *cachedModelURL = nil;
static NSManagedObjectModel *cachedModel = nil;
static NSDate *cachedModelModificationDate =nil;
// Returns the managed object model. The last read model is cached in a global variable and reused if the URL and modification date are identical
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
return __managedObjectModel;
NSDictionary *modelFileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[self.modelURL path] error:nil];
NSDate *modelModificationDate = [modelFileAttributes objectForKey:NSFileModificationDate];
if ([cachedModelURL isEqual:self.modelURL] && [modelModificationDate isEqualToDate:cachedModelModificationDate]) {
__managedObjectModel = [cachedModel retain];
}
if (!__managedObjectModel) {
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:self.modelURL];
if (!__managedObjectModel) {
NSLog(@"%@:%@ unable to load model at URL %@", [self class], NSStringFromSelector(_cmd), self.modelURL);
return nil;
}
// Clear out all custom classes used by the model to avoid having to link them
// with the importer. Remove this code if you need to access your custom logic.
NSString *managedObjectClassName = [NSManagedObject className];
for (NSEntityDescription *entity in __managedObjectModel) {
[entity setManagedObjectClassName:managedObjectClassName];
}
// cache last loaded model
[cachedModelURL release];
cachedModelURL = [self.modelURL retain];
[cachedModel release];
cachedModel = [__managedObjectModel retain];
[cachedModelModificationDate release];
cachedModelModificationDate = [modelModificationDate retain];
}
return __managedObjectModel;
}
// Returns the persistent store coordinator for the importer.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator)
return __persistentStoreCoordinator;
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:YOUR_STORE_TYPE configuration:nil URL:self.storeURL options:nil error:&error]) {
NSLog(@"%@:%@ unable to add persistent store coordinator - %@", [self class], NSStringFromSelector(_cmd), error);
}
return __persistentStoreCoordinator;
}
// Returns the managed object context for the importer; already bound to the persistent store coordinator.
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext)
return __managedObjectContext;
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
NSLog(@"%@:%@ unable to get persistent store coordinator", [self class], NSStringFromSelector(_cmd));
return nil;
}
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
return __managedObjectContext;
}
@end