GNUstep Core Data  0.1
NSManagedObjectID.m
1 /* Implementation of the NSManagedObjectID class for the GNUstep
2  Core Data framework.
3  Copyright (C) 2005 Free Software Foundation, Inc.
4 
5  Written by: Saso Kiselkov <diablos@manga.sk>
6  Date: August 2005
7 
8  This file is part of the GNUstep Core Data framework.
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 2.1 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, write to the Free
22  Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
23  */
24 
25 #import "CoreDataHeaders.h"
26 #import "GSPersistentStore.h"
27 
28 @interface NSManagedObjectID (GSCoreDataInternal)
29 
30 + (void) willBecomeMultiThreaded: (NSNotification *) notif;
31 
32 @end
33 
38 @implementation NSManagedObjectID
39 
40 + (void) initialize
41 {
42  if (self == [NSManagedObjectID class])
43  {
44  [[NSNotificationCenter defaultCenter]
45  addObserver: self
46  selector: @selector(willBecomeMultiThreaded:)
47  name: NSWillBecomeMultiThreadedNotification
48  object: nil];
49  }
50 }
51 
52 - (void) dealloc
53 {
54  TEST_RELEASE(_persistentStore);
55  TEST_RELEASE(_entity);
56 
57  [super dealloc];
58 }
59 
64 - (NSEntityDescription *) entity
65 {
66  return _entity;
67 }
68 
74 - (BOOL) isTemporaryID
75 {
76  return (_persistentStore == nil);
77 }
78 
83 - (id) persistentStore
84 {
85  return _persistentStore;
86 }
87 
93 - (NSURL *) URIRepresentation
94 {
95  if (_persistentStore == nil)
96  {
97  return nil;
98  }
99  else
100  {
101  NSString * UUID = [[_persistentStore metadata]
102  objectForKey: NSStoreUUIDKey];
103 
104  return [NSURL URLWithString: [NSString stringWithFormat:
105  @"%@/%@/%llX", UUID, [_entity name], _value]];
106  }
107 }
108 
116 - (BOOL) _isEqualToManagedObjectID: (NSManagedObjectID *) otherID
117 {
118  if ([_entity isEqual: [otherID entity]] == NO)
119  {
120  return NO;
121  }
122 
123  if ([self isTemporaryID] != [otherID isTemporaryID])
124  {
125  return NO;
126  }
127 
128  if (_persistentStore != [otherID persistentStore])
129  {
130  return NO;
131  }
132 
133  return YES;
134 }
135 
139 - (BOOL) isEqual: (id) otherObject
140 {
141  if ([otherObject isKindOfClass: [NSManagedObjectID class]])
142  {
143  return [self isEqualToManagedObjectID: otherObject];
144  }
145  else
146  {
147  return NO;
148  }
149 }
150 
151 // NSCopying
152 
153 - (id) copyWithZone: (NSZone *) zone
154 {
155  return [[NSManagedObjectID allocWithZone: zone]
156  _initWithEntity: _entity
157  persistentStore: _persistentStore
158  value: _value];
159 }
160 
161 @end
162 
163 @implementation NSManagedObjectID (GSCoreDataPrivate)
164 
171 static unsigned long long nextTemporaryID = 0;
172 
176 static NSRecursiveLock * lock = nil;
177 
178 - (id) _initWithEntity: (NSEntityDescription *) entity
179 {
180  if ((self = [super init]))
181  {
182  ASSIGN(_entity, entity);
183 
184  // make sure new temporary object IDs are generated uniquely
185  if (lock != nil)
186  {
187  [lock lock];
188 
189  _value = nextTemporaryID;
190  nextTemporaryID++;
191 
192  [lock unlock];
193  }
194  else
195  {
196  _value = nextTemporaryID;
197  nextTemporaryID++;
198  }
199  }
200  return self;
201 }
202 
203 - (id) _initWithEntity: (NSEntityDescription *) entity
204  persistentStore: (GSPersistentStore *) persistentStore
205  value: (unsigned long long) value
206 {
207  if ((self = [super init]))
208  {
209  ASSIGN(_entity, entity);
210  ASSIGN(_persistentStore, persistentStore);
211  _value = value;
212 
213  }
214  return self;
215 }
216 
217 - (unsigned long long) _value
218 {
219  return _value;
220 }
221 
222 @end
223 
224 @implementation NSManagedObjectID (GSCoreDataInternal)
225 
231 + (void) willBecomeMultiThreaded: (NSNotification *) notif
232 {
233  lock = [NSRecursiveLock new];
234 
235  [[NSNotificationCenter defaultCenter] removeObserver: self];
236 }
237 
238 @end
Nn abstract superclass from which concrete implementations of various persistent store types are subc...
For implementation notes see "Documentation/NSManagedObjectID.txt" in the source distribution of the ...
BOOL isTemporaryID()
Returns NO if the receiver is a permanent ID (that is, it has been already saved to or fetched from a...
id persistentStore()
Returns the persistent store from which the receiver has been fetched or to which it has been stored,...
NSURL * URIRepresentation()
Returns an archivable URI representation of the receiver.
NSEntityDescription * entity()
Returns the receiver's entity (that is, the entity of the object to which this managed object ID belo...