fix notification for persistent store change notification

This commit is contained in:
Christopher Lloyd 2010-10-28 21:05:59 -04:00
parent ca5d6a690a
commit 312faeac97
6 changed files with 29 additions and 18 deletions

View File

@ -12,7 +12,7 @@
-initWithPersistentStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator configurationName:(NSString *)configurationName URL:(NSURL *)url options:(NSDictionary *)options {
if([super initWithPersistentStoreCoordinator:coordinator configurationName:configurationName URL:url options:options]==nil)
return nil;
_metadata=[[NSDictionary alloc] init];
_cacheNodes=[[NSMutableSet alloc] init];
_objectIDToCacheNode=[[NSMutableDictionary alloc] init];
@ -62,7 +62,7 @@
-(NSManagedObjectID *)objectIDForEntity:(NSEntityDescription *)entity referenceObject:referenceObject {
NSMutableDictionary *refTable=[_objectIDTable objectForKey:[entity name]];
if(refTable==nil){
refTable=[NSMutableDictionary dictionary];
[_objectIDTable setObject:refTable forKey:[entity name]];
@ -76,7 +76,7 @@
[result setReferenceObject:referenceObject];
[result setStoreIdentifier:[self identifier]];
[result setPersistentStore:self];
[refTable setObject:result forKey:referenceObject];
}

View File

@ -418,7 +418,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
[values addEntriesFromDictionary:_changedValues];
return [NSString stringWithFormat:@"<%@ %x:entity name=%@, values=%@>",isa,self,[self entity],values];
return [NSString stringWithFormat:@"<%@ %x:objectID=%@ entity name=%@, values=%@>",isa,self,_objectID,[self entity],values];
}
@end

View File

@ -85,14 +85,21 @@ NSString * const NSInvalidatedAllObjectsKey=@"NSInvalidatedAllObjectsKey";
-(void)persistentStoresDidChange:(NSNotification *)note {
NSArray *stores=[[note userInfo] objectForKey:NSRemovedPersistentStoresKey];
for(NSPersistentStore *store in stores){
NSArray *allObjects=NSAllMapTableValues(_objectIdToObject);
for(NSManagedObject *check in allObjects){
NSManagedObjectID *objectID=[check objectID];
if([objectID persistentStore]==store){
NSEntityDescription *entity=[check entity];
NSArray *properties=[[entity propertiesByName] allKeys];
for(NSString *key in properties)
[check removeObserver:self forKeyPath:key];
[_registeredObjects removeObject:check];
[_insertedObjects removeObject:check];
[_updatedObjects removeObject:check];
@ -222,7 +229,7 @@ NSString * const NSInvalidatedAllObjectsKey=@"NSInvalidatedAllObjectsKey";
-(NSManagedObject *)objectWithID:(NSManagedObjectID *)objectID {
NSManagedObject *result=NSMapGet(_objectIdToObject,objectID);
if(result==nil){
result=[[NSManagedObject alloc] initWithObjectID:objectID managedObjectContext:self];
NSMapInsert(_objectIdToObject,objectID,result);
@ -235,7 +242,7 @@ NSString * const NSInvalidatedAllObjectsKey=@"NSInvalidatedAllObjectsKey";
-(NSArray *)executeFetchRequest:(NSFetchRequest *)fetchRequest error:(NSError **)error {
NSArray *affectedStores=[fetchRequest affectedStores];
if(affectedStores==nil)
affectedStores=[_storeCoordinator persistentStores];
@ -246,8 +253,9 @@ NSString * const NSInvalidatedAllObjectsKey=@"NSInvalidatedAllObjectsKey";
if([entity _isKindOfEntity:[fetchRequest entity]]){
if(![_deletedObjects containsObject:check]){
if([affectedStores containsObject:[[check objectID] persistentStore]])
if([affectedStores containsObject:[[check objectID] persistentStore]]){
[resultSet addObject:check];
}
}
}
}
@ -261,13 +269,14 @@ NSString * const NSInvalidatedAllObjectsKey=@"NSInvalidatedAllObjectsKey";
if([entity _isKindOfEntity:[fetchRequest entity]]){
NSManagedObject *check=[self objectWithID:checkID];
if(![_deletedObjects containsObject:check])
if(![_deletedObjects containsObject:check]){
[resultSet addObject:check];
}
}
}
}
NSMutableArray *result=[NSMutableArray arrayWithArray:[resultSet allObjects]];
NSPredicate *p=[fetchRequest predicate];
@ -358,7 +367,8 @@ NSString * const NSInvalidatedAllObjectsKey=@"NSInvalidatedAllObjectsKey";
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
[_updatedObjects addObject:object];
if(NSMapGet(_objectIdToObject,[object objectID])==object)
[_updatedObjects addObject:object];
}
-(BOOL)obtainPermanentIDsForObjects:(NSArray *)objects error:(NSError **)error {
@ -451,7 +461,7 @@ NSString * const NSInvalidatedAllObjectsKey=@"NSInvalidatedAllObjectsKey";
for(NSManagedObject *updated in _updatedObjects){
NSAtomicStore *store=(NSAtomicStore *)[_storeCoordinator _persistentStoreForObject:updated];
NSAtomicStoreCacheNode *node=[store cacheNodeForObjectID:[updated objectID]];
[store updateCacheNode:node fromManagedObject:updated];
[affectedStores addObject:store];

View File

@ -105,7 +105,7 @@
}
-(NSString *)description {
return [NSString stringWithFormat:@"<%@ %s URL=%@>",isa,_cmd,[self URL]];
return [NSString stringWithFormat:@"<%@ %p URL=%@>",isa,self,[self URL]];
}
@end

View File

@ -96,7 +96,7 @@ static NSMutableDictionary *_storeTypes=nil;
[store willRemoveFromPersistentStoreCoordinator:self];
[[NSNotificationCenter defaultCenter] postNotificationName:NSPersistentStoreCoordinatorStoresDidChangeNotification object:store userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:NSPersistentStoreCoordinatorStoresDidChangeNotification object:self userInfo:userInfo];
[_stores removeObjectIdenticalTo:store];

View File

@ -312,7 +312,7 @@
NSDictionary *attributesByName=[[managedObject entity] attributesByName];
NSArray *attributeKeys=[attributesByName allKeys];
NSMutableArray *children=[NSMutableArray array];
for(NSString *attributeName in attributeKeys){
NSAttributeDescription *attributeDescription=[attributesByName objectForKey:attributeName];
NSXMLElement *attributeElement=[NSXMLNode elementWithName:@"attribute"];
@ -418,7 +418,8 @@
[relationshipElement addAttribute:[NSXMLNode attributeWithName:@"destination" stringValue:[destinationEntity name]]];
NSMutableArray *idrefArray=[NSMutableArray array];
for(NSManagedObjectID *objectID in valueSet){
id referenceObject=[self referenceObjectForObjectID:objectID];