Skip to content

Commit 680ca66

Browse files
committed
Update RKTestFactory to silence logging output during setup/tear down operations. closes RestKit#764
* Introduced new logging helpers for silencing components. * Check for existence of data store at path before firing deletion to avoid log warning on failure. * Silence logging for reachability and cache during factory initialization of RKClient and RKObjectManager * Adjust log levels on cache components from info to debug
1 parent 992bfb9 commit 680ca66

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

Code/CoreData/RKEntityByAttributeCache.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ - (BOOL)shouldCoerceAttributeToString:(NSString *)attributeValue
9999

100100
- (void)load
101101
{
102-
RKLogInfo(@"Loading entity cache for Entity '%@' by attribute '%@'", self.entity.name, self.attribute);
102+
RKLogDebug(@"Loading entity cache for Entity '%@' by attribute '%@'", self.entity.name, self.attribute);
103103
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
104104
[fetchRequest setEntity:self.entity];
105105
[fetchRequest setResultType:NSManagedObjectIDResultType];
@@ -126,7 +126,7 @@ - (void)load
126126

127127
- (void)flush
128128
{
129-
RKLogInfo(@"Flushing entity cache for Entity '%@' by attribute '%@'", self.entity.name, self.attribute);
129+
RKLogDebug(@"Flushing entity cache for Entity '%@' by attribute '%@'", self.entity.name, self.attribute);
130130
self.attributeValuesToObjectIDs = nil;
131131
}
132132

Code/Support/RKLog.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ lcl_configure_by_name("App", level);
138138
} \
139139
} while(false);
140140

141+
/**
142+
Temporarily turns off logging for the given logging component during execution of the block.
143+
After the block has finished execution, the logging level is restored to its previous state.
144+
*/
145+
#define RKLogSilenceComponentWhileExecutingBlock(component, _block) \
146+
RKLogToComponentWithLevelWhileExecutingBlock(component, RKLogLevelOff, _block)
141147

142148
/**
143149
Temporarily changes the logging level for the configured RKLogComponent and executes the block. Any logging
@@ -149,7 +155,7 @@ lcl_configure_by_name("App", level);
149155

150156

151157
/**
152-
Temporarily turns off logging for the execution of the block.
158+
Temporarily turns off logging for current logging component during execution of the block.
153159
After the block has finished execution, the logging level is restored to its previous state.
154160
*/
155161
#define RKLogSilenceWhileExecutingBlock(_block) \

Code/Testing/RKTestFactory.m

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,32 @@ - (id)objectFromFactory:(NSString *)factoryName
7777
- (void)defineDefaultFactories
7878
{
7979
[self defineFactory:RKTestFactoryDefaultNamesClient withBlock:^id {
80-
RKClient *client = [RKClient clientWithBaseURL:self.baseURL];
81-
client.requestQueue.suspended = NO;
82-
[client.reachabilityObserver getFlags];
80+
__block RKClient *client;
81+
82+
RKLogSilenceComponentWhileExecutingBlock(lcl_cRestKitNetworkReachability, ^{
83+
RKLogSilenceComponentWhileExecutingBlock(lcl_cRestKitSupport, ^{
84+
client = [RKClient clientWithBaseURL:self.baseURL];
85+
client.requestQueue.suspended = NO;
86+
[client.reachabilityObserver getFlags];
87+
});
88+
});
8389

8490
return client;
8591
}];
8692

8793
[self defineFactory:RKTestFactoryDefaultNamesObjectManager withBlock:^id {
88-
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:self.baseURL];
89-
RKObjectMappingProvider *mappingProvider = [self objectFromFactory:RKTestFactoryDefaultNamesMappingProvider];
90-
objectManager.mappingProvider = mappingProvider;
91-
92-
// Force reachability determination
93-
[objectManager.client.reachabilityObserver getFlags];
94+
__block RKObjectManager *objectManager;
95+
96+
RKLogSilenceComponentWhileExecutingBlock(lcl_cRestKitNetworkReachability, ^{
97+
RKLogSilenceComponentWhileExecutingBlock(lcl_cRestKitSupport, ^{
98+
objectManager = [RKObjectManager managerWithBaseURL:self.baseURL];
99+
RKObjectMappingProvider *mappingProvider = [self objectFromFactory:RKTestFactoryDefaultNamesMappingProvider];
100+
objectManager.mappingProvider = mappingProvider;
101+
102+
// Force reachability determination
103+
[objectManager.client.reachabilityObserver getFlags];
104+
});
105+
});
94106

95107
return objectManager;
96108
}];
@@ -194,7 +206,12 @@ + (void)setUp
194206
{
195207
[RKObjectManager setDefaultMappingQueue:dispatch_queue_create("org.restkit.ObjectMapping", DISPATCH_QUEUE_SERIAL)];
196208
[RKObjectMapping setDefaultDateFormatters:nil];
197-
[RKManagedObjectStore deleteStoreInApplicationDataDirectoryWithFilename:RKTestFactoryDefaultStoreFilename];
209+
210+
// Delete the store if it exists
211+
NSString *path = [[RKDirectory applicationDataDirectory] stringByAppendingPathComponent:RKTestFactoryDefaultStoreFilename];
212+
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
213+
[RKManagedObjectStore deleteStoreInApplicationDataDirectoryWithFilename:RKTestFactoryDefaultStoreFilename];
214+
}
198215

199216
if ([self respondsToSelector:@selector(didSetUp)]) {
200217
[self didSetUp];
@@ -218,7 +235,7 @@ + (void)clearCacheDirectory
218235
NSString* cachePath = [RKDirectory cachesDirectory];
219236
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:cachePath error:&error];
220237
if (success) {
221-
RKLogInfo(@"Cleared cache directory...");
238+
RKLogDebug(@"Cleared cache directory...");
222239
success = [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:&error];
223240
if (!success) {
224241
RKLogError(@"Failed creation of cache path '%@': %@", cachePath, [error localizedDescription]);

0 commit comments

Comments
 (0)