How can I remove an NSPersistentStore from my coordinator once it is unused?
I'm using in-memory NSPersistentStores to hold transient objects.  Once the store is unused - which doesn't necessarily mean empty - I'd like to remove it from the coordinator to free up the memory it uses.

My first attempt was to have each controller create a store in its init, and remove it in its dealloc.  This didn't work, because background threads were still using NSManagedObjects in that store.

My second attempt was to wrap the stores in an object that could remove the real store once the wrapped object's retain count hit zero, like so:

    @interface MyStoreWrapper : NSObject

    @property (nonatomic, retain) NSPersistentStore *persistentStore;

    +(MyStoreWrapper *) wrappedInMemoryStore;
    +(MyStoreWrapper *) wrapStore:(NSPersistentStore *)aStore;
    -(id) initWithStore:(NSPersistentStore *)aStore;

    @end


    @implementation MyStoreWrapper

    @synthesize persistentStore;

    +(MyStoreWrapper *)wrappedInMemoryStore
    {
        NSError *error = nil;
        NSPersistentStore *store = [[[MyAppDelegate sharedDelegate] persistentStoreCoordinator] addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:&error];
        
        if (error)
        {
            [NSException raise:@"Core data error" format:@"Could not add temporary store: %@, %@", error, [error userInfo]];
        }
        
        return [self wrapStore:store];
    }

    +(MyStoreWrapper *)wrapStore:(NSPersistentStore *)aStore
    {
        return [[[self alloc] initWithStore:aStore] autorelease];
    }

    -(id)initWithStore:(NSPersistentStore *)aStore
    {
        self = [super init];
        if (self)
        {
            self.persistentStore = aStore;
        }
        return self;
    }

    -(void)dealloc
    {
        NSError *error = nil;
        [[[MyAppDelegate sharedDelegate] persistentStoreCoordinator] removePersistentStore:self.persistentStore error:&error];
        
        if (error)
        {
            [NSException raise:@"Core data error" format:@"Could not remove temporary store: %@, %@", error, [error userInfo]];
        }
        
        [super dealloc];
    }

    - (void)forwardInvocation:(NSInvocation *)anInvocation
    {
        if ([self.persistentStore respondsToSelector:[anInvocation selector]])
        {
            [anInvocation invokeWithTarget:self.persistentStore];
        }
        else
        {   
            [super forwardInvocation:anInvocation];
        }
    }

    - (BOOL)respondsToSelector:(SEL)aSelector
    {
        if ([super respondsToSelector:aSelector])
        {
            return YES;
        }
        else
        {
            return [self.persistentStore respondsToSelector:aSelector];
        }
    }

    - (NSMethodSignature*)methodSignatureForSelector:(SEL)aSelector
    {
        NSMethodSignature* signature = [super methodSignatureForSelector:aSelector];
        
        if (!signature)
        {
            signature = [self.persistentStore methodSignatureForSelector:aSelector];    
        }
        
        return signature;    
    }

    +(BOOL)instancesRespondToSelector:(SEL)aSelector
    {
        if ([super instancesRespondToSelector:aSelector])
        {
            return YES;
        }
        else
        {
            return [NSPersistentStore instancesRespondToSelector:aSelector];
        }
    }

    +(NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector
    {
        NSMethodSignature* signature = [super instanceMethodSignatureForSelector:aSelector];
        
        if (!signature)
        {
            signature = [NSPersistentStore instanceMethodSignatureForSelector:aSelector];    
        }
        
        return signature;   
    }

    @end

...but this wrapped object doesn't seem to be a valid substitute in e.g. NSFetchRequests.

Have I made some mistake in my wrapper class?  Is there some other way to remove an NSPersistentStore from my coordinator once it is unused?
How can I remove an NSPersistentStore from my coordinator once it is unused?
===

I'm using in-memory NSPersistentStores to hold transient objects.  Once the store is unused - which doesn't necessarily mean empty - I'd like to remove it from the coordinator to free up the memory it uses.

My first attempt was to have each controller create a store in its init, and remove it in its dealloc.  This didn't work, because background threads were still using NSManagedObjects in that store.

My second attempt was to wrap the stores in an object that could remove the real store once the wrapped object's retain count hit zero, like so:

    @interface MyStoreWrapper : NSObject

    @property (nonatomic, retain) NSPersistentStore *persistentStore;

    +(MyStoreWrapper *) wrappedInMemoryStore;
    +(MyStoreWrapper *) wrapStore:(NSPersistentStore *)aStore;
    -(id) initWithStore:(NSPersistentStore *)aStore;

    @end


    @implementation MyStoreWrapper

    @synthesize persistentStore;

    +(MyStoreWrapper *)wrappedInMemoryStore
    {
        NSError *error = nil;
        NSPersistentStore *store = [[[MyAppDelegate sharedDelegate] persistentStoreCoordinator] addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:&error];
        
        if (error)
        {
            [NSException raise:@"Core data error" format:@"Could not add temporary store: %@, %@", error, [error userInfo]];
        }
        
        return [self wrapStore:store];
    }

    +(MyStoreWrapper *)wrapStore:(NSPersistentStore *)aStore
    {
        return [[[self alloc] initWithStore:aStore] autorelease];
    }

    -(id)initWithStore:(NSPersistentStore *)aStore
    {
        self = [super init];
        if (self)
        {
            self.persistentStore = aStore;
        }
        return self;
    }

    -(void)dealloc
    {
        NSError *error = nil;
        [[[MyAppDelegate sharedDelegate] persistentStoreCoordinator] removePersistentStore:self.persistentStore error:&error];
        
        if (error)
        {
            [NSException raise:@"Core data error" format:@"Could not remove temporary store: %@, %@", error, [error userInfo]];
        }
        
        [super dealloc];
    }

    - (void)forwardInvocation:(NSInvocation *)anInvocation
    {
        if ([self.persistentStore respondsToSelector:[anInvocation selector]])
        {
            [anInvocation invokeWithTarget:self.persistentStore];
        }
        else
        {   
            [super forwardInvocation:anInvocation];
        }
    }

    - (BOOL)respondsToSelector:(SEL)aSelector
    {
        if ([super respondsToSelector:aSelector])
        {
            return YES;
        }
        else
        {
            return [self.persistentStore respondsToSelector:aSelector];
        }
    }

    - (NSMethodSignature*)methodSignatureForSelector:(SEL)aSelector
    {
        NSMethodSignature* signature = [super methodSignatureForSelector:aSelector];
        
        if (!signature)
        {
            signature = [self.persistentStore methodSignatureForSelector:aSelector];    
        }
        
        return signature;    
    }

    +(BOOL)instancesRespondToSelector:(SEL)aSelector
    {
        if ([super instancesRespondToSelector:aSelector])
        {
            return YES;
        }
        else
        {
            return [NSPersistentStore instancesRespondToSelector:aSelector];
        }
    }

    +(NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector
    {
        NSMethodSignature* signature = [super instanceMethodSignatureForSelector:aSelector];
        
        if (!signature)
        {
            signature = [NSPersistentStore instanceMethodSignatureForSelector:aSelector];    
        }
        
        return signature;   
    }

    @end

...but this wrapped object doesn't seem to be a valid substitute in e.g. NSFetchRequests.

Have I made some mistake in my wrapper class?  Is there some other way to remove an NSPersistentStore from my coordinator once it is unused?