GNUstep Core Data  0.1
NSManagedObjectID.m
00001 /* Implementation of the NSManagedObjectID class for the GNUstep
00002    Core Data framework.
00003    Copyright (C) 2005 Free Software Foundation, Inc.
00004 
00005    Written by:  Saso Kiselkov <diablos@manga.sk>
00006    Date: August 2005
00007 
00008    This file is part of the GNUstep Core Data framework.
00009 
00010    This library is free software; you can redistribute it and/or
00011    modify it under the terms of the GNU Lesser General Public
00012    License as published by the Free Software Foundation; either
00013    version 2.1 of the License, or (at your option) any later version.
00014 
00015    This library is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018    Lesser General Public License for more details.
00019 
00020    You should have received a copy of the GNU Lesser General Public
00021    License along with this library; if not, write to the Free
00022    Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
00023  */
00024 
00025 #import "CoreDataHeaders.h"
00026 #import "GSPersistentStore.h"
00027 
00028 @interface NSManagedObjectID (GSCoreDataInternal)
00029 
00030 + (void) willBecomeMultiThreaded: (NSNotification *) notif;
00031 
00032 @end
00033 
00038 @implementation NSManagedObjectID
00039 
00040 + (void) initialize
00041 {
00042   if (self == [NSManagedObjectID class])
00043     {
00044       [[NSNotificationCenter defaultCenter]
00045         addObserver: self
00046            selector: @selector(willBecomeMultiThreaded:)
00047                name: NSWillBecomeMultiThreadedNotification
00048              object: nil];
00049     }
00050 }
00051 
00052 - (void) dealloc
00053 {
00054   TEST_RELEASE(_persistentStore);
00055   TEST_RELEASE(_entity);
00056 
00057   [super dealloc];
00058 }
00059 
00064 - (NSEntityDescription *) entity
00065 {
00066   return _entity;
00067 }
00068 
00074 - (BOOL) isTemporaryID
00075 {
00076   return (_persistentStore == nil);
00077 }
00078 
00083 - (id) persistentStore
00084 {
00085   return _persistentStore;
00086 }
00087 
00093 - (NSURL *) URIRepresentation
00094 {
00095   if (_persistentStore == nil)
00096     {
00097       return nil;
00098     }
00099   else
00100     {
00101       NSString * UUID = [[_persistentStore metadata]
00102         objectForKey: NSStoreUUIDKey];
00103 
00104       return [NSURL URLWithString: [NSString stringWithFormat:
00105         @"%@/%@/%llX", UUID, [_entity name], _value]];
00106     }
00107 }
00108 
00116 - (BOOL) _isEqualToManagedObjectID: (NSManagedObjectID *) otherID
00117 {
00118   if ([_entity isEqual: [otherID entity]] == NO)
00119     {
00120       return NO;
00121     }
00122 
00123   if ([self isTemporaryID] != [otherID isTemporaryID])
00124     {
00125       return NO;
00126     }
00127 
00128   if (_persistentStore != [otherID persistentStore])
00129     {
00130       return NO;
00131     }
00132 
00133   return YES;
00134 }
00135 
00139 - (BOOL) isEqual: (id) otherObject
00140 {
00141   if ([otherObject isKindOfClass: [NSManagedObjectID class]])
00142     {
00143       return [self isEqualToManagedObjectID: otherObject];
00144     }
00145   else
00146     {
00147       return NO;
00148     }
00149 }
00150 
00151 // NSCopying
00152 
00153 - (id) copyWithZone: (NSZone *) zone
00154 {
00155   return [[NSManagedObjectID allocWithZone: zone]
00156     _initWithEntity: _entity
00157     persistentStore: _persistentStore
00158               value: _value];
00159 }
00160 
00161 @end
00162 
00163 @implementation NSManagedObjectID (GSCoreDataPrivate)
00164 
00171 static unsigned long long nextTemporaryID = 0;
00172 
00176 static NSRecursiveLock * lock = nil;
00177 
00178 - (id) _initWithEntity: (NSEntityDescription *) entity
00179 {
00180         if ((self = [super init]))
00181     {
00182       ASSIGN(_entity, entity);
00183 
00184       // make sure new temporary object IDs are generated uniquely
00185       if (lock != nil)
00186         {
00187           [lock lock];
00188 
00189           _value = nextTemporaryID;
00190           nextTemporaryID++;
00191 
00192           [lock unlock];
00193         }
00194       else
00195         {
00196           _value = nextTemporaryID;
00197           nextTemporaryID++;
00198         }
00199         }
00200       return self;
00201 }
00202 
00203 - (id) _initWithEntity: (NSEntityDescription *) entity
00204        persistentStore: (GSPersistentStore *) persistentStore
00205                  value: (unsigned long long) value
00206 {
00207         if ((self = [super init]))
00208     {
00209       ASSIGN(_entity, entity);
00210       ASSIGN(_persistentStore, persistentStore);
00211       _value = value;
00212 
00213         }
00214         return self;
00215 }
00216 
00217 - (unsigned long long) _value
00218 {
00219   return _value;
00220 }
00221 
00222 @end
00223 
00224 @implementation NSManagedObjectID (GSCoreDataInternal)
00225 
00231 + (void) willBecomeMultiThreaded: (NSNotification *) notif
00232 {
00233   lock = [NSRecursiveLock new];
00234 
00235   [[NSNotificationCenter defaultCenter] removeObserver: self];
00236 }
00237 
00238 @end