mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-28 20:55:32 +08:00
Add support for shared factory object instances
This commit is contained in:
@@ -141,6 +141,16 @@ extern NSString * const RKTestFactoryDefaultNamesManagedObjectStore;
|
||||
+ (id)objectFromFactory:(NSString *)factoryName properties:(NSDictionary *)properties;
|
||||
+ (id)objectFromFactory:(NSString *)factoryName;
|
||||
|
||||
/**
|
||||
Fetches a shared object from the factory with the given name. If an existing object has already been created, then that instance is returned. If a shared instance does not yet exist, one will be constructed and returned for this and all subsequent invocations of `sharedObjectFromFactory:`. Shared object instances are discarded when the factory is torn down.
|
||||
|
||||
Shared objects are used to return object instances for cases where it does not make sense to instantiate a new instance on every invocation of the factory. A common example where this is appropriate is the `managedObjectStore` factory, where construction of a new store on each invocation would yield managed objects that cross Core Data stacks.
|
||||
|
||||
@param factoryName The name of the factory to retrieve the shared instance of.
|
||||
@return The shared object instance for the factory registered with the given name.
|
||||
*/
|
||||
+ (id)sharedObjectFromFactory:(NSString *)factoryName;
|
||||
|
||||
/**
|
||||
Inserts a new managed object for the `NSEntityDescription` with the given name into the specified managed object context and sets properties on the instance from the given dictionary. A permanent managed object ID is obtained for the object so that it can be referenced across threads without any further work.
|
||||
|
||||
@@ -160,30 +170,30 @@ extern NSString * const RKTestFactoryDefaultNamesManagedObjectStore;
|
||||
*/
|
||||
+ (NSSet *)factoryNames;
|
||||
|
||||
///-------------------------
|
||||
/// @name Building Instances
|
||||
///-------------------------
|
||||
///--------------------------------
|
||||
/// @name Retrieving Shared Objects
|
||||
///--------------------------------
|
||||
|
||||
/**
|
||||
Creates and returns an `AFHTTPClient` object using the factory defined for the name `RKTestFactoryDefaultNamesClient`.
|
||||
Fetches the shared `AFHTTPClient` object using the factory defined for the name `RKTestFactoryDefaultNamesClient`.
|
||||
|
||||
@return A new client instance.
|
||||
@return The shared client instance.
|
||||
*/
|
||||
+ (id)client;
|
||||
|
||||
/**
|
||||
Creates and returns an `RKObjectManager` object using the factory defined for the name `RKTestFactoryDefaultNamesObjectManager`.
|
||||
Fetches the shared `RKObjectManager` object using the factory defined for the name `RKTestFactoryDefaultNamesObjectManager`.
|
||||
|
||||
@return A new object manager instance.
|
||||
@return The shared object manager instance.
|
||||
*/
|
||||
+ (id)objectManager;
|
||||
|
||||
/**
|
||||
Creates and returns an `RKManagedObjectStore` object using the factory defined for the name `RKTestFactoryDefaultNamesManagedObjectStore`.
|
||||
Fetches the shared an `RKManagedObjectStore` object using the factory defined for the name `RKTestFactoryDefaultNamesManagedObjectStore`.
|
||||
|
||||
A new managed object store will be configured and returned. If there is an existing persistent store (i.e. from a previous test invocation), then the persistent store is deleted.
|
||||
On first invocation per factory setup/teardown, a new managed object store will be configured and returned. If there is an existing persistent store (i.e. from a previous test invocation), then the persistent store is deleted.
|
||||
|
||||
@return A new managed object store instance.
|
||||
@return The shared managed object store instance.
|
||||
*/
|
||||
+ (id)managedObjectStore;
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
@property (nonatomic, strong) NSURL *baseURL;
|
||||
@property (nonatomic, strong) NSString *managedObjectStoreFilename;
|
||||
@property (nonatomic, strong) NSMutableDictionary *factoryBlocks;
|
||||
@property (nonatomic, strong) NSMutableDictionary *sharedObjectsByFactoryName;
|
||||
|
||||
+ (RKTestFactory *)sharedFactory;
|
||||
- (void)defineFactory:(NSString *)factoryName withBlock:(id (^)())block;
|
||||
@@ -56,6 +57,7 @@ static RKTestFactory *sharedFactory = nil;
|
||||
self.baseURL = [NSURL URLWithString:@"http://127.0.0.1:4567"];
|
||||
self.managedObjectStoreFilename = RKTestFactoryDefaultStoreFilename;
|
||||
self.factoryBlocks = [NSMutableDictionary new];
|
||||
self.sharedObjectsByFactoryName = [NSMutableDictionary new];
|
||||
[self defineDefaultFactories];
|
||||
}
|
||||
|
||||
@@ -77,6 +79,16 @@ static RKTestFactory *sharedFactory = nil;
|
||||
return object;
|
||||
}
|
||||
|
||||
- (id)sharedObjectFromFactory:(NSString *)factoryName
|
||||
{
|
||||
id sharedObject = [self.sharedObjectsByFactoryName objectForKey:factoryName];
|
||||
if (! sharedObject) {
|
||||
sharedObject = [self objectFromFactory:factoryName properties:nil];
|
||||
[self.sharedObjectsByFactoryName setObject:sharedObject forKey:factoryName];
|
||||
}
|
||||
return sharedObject;
|
||||
}
|
||||
|
||||
- (void)defineDefaultFactories
|
||||
{
|
||||
[self defineFactory:RKTestFactoryDefaultNamesClient withBlock:^id {
|
||||
@@ -160,6 +172,11 @@ static RKTestFactory *sharedFactory = nil;
|
||||
return [[RKTestFactory sharedFactory] objectFromFactory:factoryName properties:nil];
|
||||
}
|
||||
|
||||
+ (id)sharedObjectFromFactory:(NSString *)factoryName
|
||||
{
|
||||
return [[RKTestFactory sharedFactory] sharedObjectFromFactory:factoryName];
|
||||
}
|
||||
|
||||
+ (id)insertManagedObjectForEntityForName:(NSString *)entityName
|
||||
inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
|
||||
withProperties:(NSDictionary *)properties
|
||||
@@ -187,21 +204,22 @@ static RKTestFactory *sharedFactory = nil;
|
||||
|
||||
+ (id)client
|
||||
{
|
||||
return [self objectFromFactory:RKTestFactoryDefaultNamesClient properties:nil];
|
||||
return [self sharedObjectFromFactory:RKTestFactoryDefaultNamesClient];
|
||||
}
|
||||
|
||||
+ (id)objectManager
|
||||
{
|
||||
return [self objectFromFactory:RKTestFactoryDefaultNamesObjectManager properties:nil];
|
||||
return [self sharedObjectFromFactory:RKTestFactoryDefaultNamesObjectManager];
|
||||
}
|
||||
|
||||
+ (id)managedObjectStore
|
||||
{
|
||||
return [self objectFromFactory:RKTestFactoryDefaultNamesManagedObjectStore properties:nil];
|
||||
return [self sharedObjectFromFactory:RKTestFactoryDefaultNamesManagedObjectStore];
|
||||
}
|
||||
|
||||
+ (void)setUp
|
||||
{
|
||||
[[RKTestFactory sharedFactory].sharedObjectsByFactoryName removeAllObjects];
|
||||
[RKObjectManager setSharedManager:nil];
|
||||
[RKManagedObjectStore setDefaultStore:nil];
|
||||
|
||||
@@ -218,6 +236,7 @@ static RKTestFactory *sharedFactory = nil;
|
||||
|
||||
+ (void)tearDown
|
||||
{
|
||||
[[RKTestFactory sharedFactory].sharedObjectsByFactoryName removeAllObjects];
|
||||
[RKObjectManager setSharedManager:nil];
|
||||
[RKManagedObjectStore setDefaultStore:nil];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user