Migrate path utilities to C functions instead of encapsulating into static methods on RKDirectory to reduce API size

This commit is contained in:
Blake Watters
2012-08-13 22:53:47 -04:00
parent 975e1674a2
commit 37c70e4c27
12 changed files with 93 additions and 106 deletions

View File

@@ -22,7 +22,7 @@
#import "RKLog.h"
#import "RKPropertyInspector.h"
#import "RKPropertyInspector+CoreData.h"
#import "RKDirectory.h"
#import "RKDirectoryUtilities.h"
#import "RKInMemoryManagedObjectCache.h"
#import "RKFetchRequestManagedObjectCache.h"
#import "NSBundle+RKAdditions.h"
@@ -364,7 +364,7 @@ static RKManagedObjectStore *defaultStore = nil;
+ (void)deleteStoreInApplicationDataDirectoryWithFilename:(NSString *)filename DEPRECATED_ATTRIBUTE
{
NSString *path = [[RKDirectory applicationDataDirectory] stringByAppendingPathComponent:filename];
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:filename];
[self deleteStoreAtPath:path];
}
@@ -400,8 +400,8 @@ static RKManagedObjectStore *defaultStore = nil;
NSString *storeDirectory = nilOrDirectoryPath;
if (storeDirectory == nil) {
// If initializing into Application Data directory, ensure the directory exists
storeDirectory = [RKDirectory applicationDataDirectory];
[RKDirectory ensureDirectoryExistsAtPath:storeDirectory error:nil];
storeDirectory = RKApplicationDataDirectory();
RKEnsureDirectoryExistsAtPath(storeDirectory, nil);
} else {
// If path given, caller is responsible for directory's existence
BOOL isDir;

View File

@@ -25,7 +25,7 @@
#import "RKLog.h"
#import "RKPathMatcher.h"
#import "NSString+RKAdditions.h"
#import "RKDirectory.h"
#import "RKDirectoryUtilities.h"
// Set Logging Component
#undef RKLogComponent
@@ -224,7 +224,7 @@ dispatch_queue_t rk_get_network_processing_queue(void)
- (NSString *)cachePath
{
NSString *cacheDirForClient = [NSString stringWithFormat:@"RKClientRequestCache-%@", [self.baseURL host]];
NSString *cachePath = [[RKDirectory cachesDirectory]
NSString *cachePath = [RKCachesDirectory()
stringByAppendingPathComponent:cacheDirForClient];
return cachePath;
}

View File

@@ -1,5 +1,5 @@
//
// RKDirectory.h
// RKDirectoryUtilities.h
// RestKit
//
// Created by Blake Watters on 12/9/11.
@@ -8,38 +8,30 @@
#import <Foundation/Foundation.h>
/**
iOS and OS X agnostic accessors for safely returning directory paths for use
by the framework and applications.
*/
@interface RKDirectory : NSObject
/**
Returns the path to the Application Data directory for the executing application. On iOS,
this is a sandboxed path specific for the executing application. On OS X, this is an application
specific path under NSApplicationSupportDirectory (i.e. ~/Application Support).
@return The full path to the application data directory.
*/
+ (NSString *)applicationDataDirectory;
NSString * RKApplicationDataDirectory(void);
/**
Returns a path to the root caches directory used by RestKit for storage. On iOS, this is
a sanboxed path specific for the executing application. On OS X, this is an application
specific path under NSCachesDirectory (i.e. ~/Library/Caches).
@return The full path to the Caches directory.
*/
+ (NSString *)cachesDirectory;
NSString * RKCachesDirectory(void);
/**
Ensures that a directory exists at a given path by checking for the existence
of the directory and creating it if it does not exist.
@param path The path to ensure a directory exists at.
@param error On input, a pointer to an error object.
@returns A Boolean value indicating if the directory exists.
*/
+ (BOOL)ensureDirectoryExistsAtPath:(NSString *)path error:(NSError **)error;
@end
BOOL RKEnsureDirectoryExistsAtPath(NSString *path, NSError **error);

View File

@@ -1,59 +1,56 @@
//
// RKDirectory.m
// RKDirectoryUtilities.m
// RestKit
//
// Created by Blake Watters on 12/9/11.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//
#import "RKDirectory.h"
#import "RKDirectoryUtilities.h"
#import "NSBundle+RKAdditions.h"
#import "RKLog.h"
@implementation RKDirectory
NSString * RKExecutableName(void);
+ (NSString *)executableName
NSString * RKApplicationDataDirectory(void)
{
#if TARGET_OS_IPHONE
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
#else
NSFileManager *sharedFM = [NSFileManager defaultManager];
NSArray *possibleURLs = [sharedFM URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
NSURL *appSupportDir = nil;
NSURL *appDirectory = nil;
if ([possibleURLs count] >= 1) {
appSupportDir = [possibleURLs objectAtIndex:0];
}
if (appSupportDir) {
NSString *executableName = [RKDirectory executableName];
appDirectory = [appSupportDir URLByAppendingPathComponent:executableName];
return [appDirectory path];
}
return nil;
#endif
}
NSString * RKExecutableName(void)
{
NSString *executableName = [[[NSBundle mainBundle] executablePath] lastPathComponent];
if (nil == executableName) {
RKLogWarning(@"Unable to determine CFBundleExecutable: storing data under RestKit directory name.");
executableName = @"RestKit";
}
return executableName;
}
+ (NSString *)applicationDataDirectory
{
#if TARGET_OS_IPHONE
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
#else
NSFileManager *sharedFM = [NSFileManager defaultManager];
NSArray *possibleURLs = [sharedFM URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
NSURL *appSupportDir = nil;
NSURL *appDirectory = nil;
if ([possibleURLs count] >= 1) {
appSupportDir = [possibleURLs objectAtIndex:0];
}
if (appSupportDir) {
NSString *executableName = [RKDirectory executableName];
appDirectory = [appSupportDir URLByAppendingPathComponent:executableName];
return [appDirectory path];
}
return nil;
#endif
}
+ (NSString *)cachesDirectory
NSString * RKCachesDirectory(void)
{
#if TARGET_OS_IPHONE
return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
@@ -63,12 +60,12 @@
if ([paths count]) {
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[RKDirectory executableName]];
}
return path;
#endif
}
+ (BOOL)ensureDirectoryExistsAtPath:(NSString *)path error:(NSError **)error
BOOL RKEnsureDirectoryExistsAtPath(NSString *path, NSError **error)
{
BOOL isDirectory;
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) {
@@ -78,15 +75,13 @@
return YES;
}
}
// Create the directory and any intermediates
NSError *errorReference = (error == nil) ? nil : *error;
if (! [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&errorReference]) {
RKLogError(@"Failed to create requested directory at path '%@': %@", path, errorReference);
return NO;
}
return YES;
}
@end

View File

@@ -24,7 +24,7 @@
#import "RKLog.h"
#import "RKPathMatcher.h"
#import "RKDotNetDateFormatter.h"
#import "RKDirectory.h"
#import "RKDirectoryUtilities.h"
#import "RKBenchmark.h"
// Load our categories

View File

@@ -227,7 +227,7 @@ extern NSString * const RKTestFactoryDefaultNamesManagedObjectStore;
Clears the contents of the cache directory by removing the directory and
recreating it.
@see [RKDirectory cachesDirectory]
@see RKCachesDirectory()
*/
+ (void)clearCacheDirectory;

View File

@@ -113,7 +113,7 @@ static RKTestFactory *sharedFactory = nil;
}];
[self defineFactory:RKTestFactoryDefaultNamesManagedObjectStore withBlock:^id {
NSString *storePath = [[RKDirectory applicationDataDirectory] stringByAppendingPathComponent:RKTestFactoryDefaultStoreFilename];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:RKTestFactoryDefaultStoreFilename];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] init];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil error:&error];
@@ -216,7 +216,7 @@ static RKTestFactory *sharedFactory = nil;
[RKObjectMapping setDefaultDateFormatters:nil];
// Delete the store if it exists
NSString *path = [[RKDirectory applicationDataDirectory] stringByAppendingPathComponent:RKTestFactoryDefaultStoreFilename];
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:RKTestFactoryDefaultStoreFilename];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
@@ -240,7 +240,7 @@ static RKTestFactory *sharedFactory = nil;
+ (void)clearCacheDirectory
{
NSError *error = nil;
NSString *cachePath = [RKDirectory cachesDirectory];
NSString *cachePath = RKCachesDirectory();
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:cachePath error:&error];
if (success) {
RKLogDebug(@"Cleared cache directory...");