mirror of
https://github.com/zhigang1992/MagicalRecord.git
synced 2026-01-12 17:32:18 +08:00
Add category prefix for persistent store and coordinator categories
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#define HC_SHORTHAND
|
||||
#import <OCHamcrest/OCHamcrest.h>
|
||||
|
||||
#define MR_SHORTHAND
|
||||
#import "CoreData+MagicalRecord.h"
|
||||
#import "FixtureHelpers.h"
|
||||
#endif
|
||||
|
||||
@@ -108,33 +108,34 @@
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef MR_SHORTHAND
|
||||
|
||||
#define defaultBatchSize MR_defaultBatchSize
|
||||
#define setDefaultBatchSize MR_setDefaultBatchSize
|
||||
|
||||
#define createFetchRequest MR_createFetchRequest
|
||||
#define executeFetchRequest MR_executeFetchRequest
|
||||
|
||||
#define requestAll MR_requestAll
|
||||
#define trucateAll MR_truncateAll
|
||||
|
||||
#define numberOfEntities MR_numberOfEntities
|
||||
#define hasAtLeastOneEntity MR_hasAtLeastOneEntity
|
||||
|
||||
#define findAll MR_findAll
|
||||
#define findByAttribute MR_findByAttribute
|
||||
|
||||
#define findFirst MR_findFirst
|
||||
#define findFirstByAttribute MR_findFirstByAttribute
|
||||
|
||||
#define fetchAllSortedBy MR_fetchAllSortedBy
|
||||
#define fetchRequest MR_fetchRequest
|
||||
|
||||
#define inContext MR_inContext
|
||||
#define inThreadContext MR_inThreadContext
|
||||
|
||||
#endif
|
||||
|
||||
@end
|
||||
|
||||
/*
|
||||
#ifdef MR_SHORTHAND
|
||||
|
||||
#define defaultBatchSize MR_defaultBatchSize
|
||||
#define setDefaultBatchSize MR_setDefaultBatchSize
|
||||
|
||||
#define createFetchRequest MR_createFetchRequest
|
||||
#define executeFetchRequest MR_executeFetchRequest
|
||||
|
||||
#define requestAll MR_requestAll
|
||||
#define trucateAll MR_truncateAll
|
||||
|
||||
#define numberOfEntities MR_numberOfEntities
|
||||
#define hasAtLeastOneEntity MR_hasAtLeastOneEntity
|
||||
|
||||
#define findAll MR_findAll
|
||||
#define findByAttribute MR_findByAttribute
|
||||
|
||||
#define findFirst MR_findFirst
|
||||
#define findFirstByAttribute MR_findFirstByAttribute
|
||||
|
||||
#define fetchAllSortedBy MR_fetchAllSortedBy
|
||||
#define fetchRequest MR_fetchRequest
|
||||
|
||||
#define inContext MR_inContext
|
||||
#define inThreadContext MR_inThreadContext
|
||||
|
||||
#endif
|
||||
*/
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
|
||||
@end
|
||||
|
||||
|
||||
/*
|
||||
#ifdef MR_SHORTHAND
|
||||
|
||||
#define observeContext MR_observeContext
|
||||
@@ -56,10 +56,12 @@
|
||||
#define setNotifiesMainContextOnSave MR_setNotifiesMainContextOnSave
|
||||
#define notifiesMainContextonSave MR_notifiesMainContextonSave
|
||||
|
||||
#define context] MR_context]
|
||||
//#define context] MR_context]
|
||||
#define contextForCurrentThread MR_contextForCurrentThread
|
||||
|
||||
#define contextThatNotifiesDefaultContextOnMainThread MR_contextThatNotifiesDefaultContextOnMainThread
|
||||
#define contextWithStoreCoordinator MR_contextWithStoreCoodinator
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
*/
|
||||
|
||||
@@ -224,7 +224,7 @@ static NSString const * kMagicalRecordManagedObjectContextKey = @"MagicalRecord_
|
||||
|
||||
+ (NSManagedObjectContext *) context
|
||||
{
|
||||
return [self contextWithStoreCoordinator:[NSPersistentStoreCoordinator defaultStoreCoordinator]];
|
||||
return [self contextWithStoreCoordinator:[NSPersistentStoreCoordinator MR_defaultStoreCoordinator]];
|
||||
}
|
||||
|
||||
+ (NSManagedObjectContext *) contextThatNotifiesDefaultContextOnMainThread
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
@end
|
||||
|
||||
/*
|
||||
#ifdef MR_SHORTHAND
|
||||
|
||||
#define defaultManagedObjectModel MR_defaultManagedObjectModel
|
||||
@@ -28,4 +29,5 @@
|
||||
#define managedObectModelNamed MR_managedObjectModelNamed
|
||||
#define newModelNamed MR_newModelNamed
|
||||
|
||||
#endif
|
||||
#endif
|
||||
*/
|
||||
@@ -12,12 +12,12 @@ extern NSString * const kMagicalRecordDefaultStoreFileName;
|
||||
|
||||
@interface NSPersistentStore (MagicalRecord)
|
||||
|
||||
+ (NSURL *) defaultLocalStoreUrl;
|
||||
+ (NSURL *) MR_defaultLocalStoreUrl;
|
||||
|
||||
+ (NSPersistentStore *) defaultPersistentStore;
|
||||
+ (void) setDefaultPersistentStore:(NSPersistentStore *) store;
|
||||
+ (NSPersistentStore *) MR_defaultPersistentStore;
|
||||
+ (void) MR_setDefaultPersistentStore:(NSPersistentStore *) store;
|
||||
|
||||
+ (NSURL *) urlForStoreName:(NSString *)storeFileName;
|
||||
+ (NSURL *) MR_urlForStoreName:(NSString *)storeFileName;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -10,47 +10,47 @@
|
||||
|
||||
NSString * const kMagicalRecordDefaultStoreFileName = @"CoreDataStore.sqlite";
|
||||
|
||||
static NSPersistentStore *defaultPersistentStore = nil;
|
||||
static NSPersistentStore *defaultPersistentStore_ = nil;
|
||||
|
||||
@implementation NSPersistentStore (MagicalRecord)
|
||||
|
||||
+ (NSPersistentStore *) defaultPersistentStore
|
||||
+ (NSPersistentStore *) MR_defaultPersistentStore
|
||||
{
|
||||
return defaultPersistentStore;
|
||||
return defaultPersistentStore_;
|
||||
}
|
||||
|
||||
+ (void) setDefaultPersistentStore:(NSPersistentStore *) store
|
||||
+ (void) MR_setDefaultPersistentStore:(NSPersistentStore *) store
|
||||
{
|
||||
defaultPersistentStore = store;
|
||||
defaultPersistentStore_ = store;
|
||||
}
|
||||
|
||||
+ (NSString *) directory:(int) type
|
||||
+ (NSString *) MR_directory:(int) type
|
||||
{
|
||||
return [NSSearchPathForDirectoriesInDomains(type, NSUserDomainMask, YES) lastObject];
|
||||
}
|
||||
|
||||
+ (NSString *)applicationDocumentsDirectory
|
||||
+ (NSString *)MR_applicationDocumentsDirectory
|
||||
{
|
||||
return [self directory:NSDocumentDirectory];
|
||||
return [self MR_directory:NSDocumentDirectory];
|
||||
}
|
||||
|
||||
+ (NSString *)applicationLibraryDirectory
|
||||
+ (NSString *)MR_applicationLibraryDirectory
|
||||
{
|
||||
#if !TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR
|
||||
|
||||
NSString *applicationName = [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleNameKey];
|
||||
return [[self directory:NSApplicationSupportDirectory] stringByAppendingPathComponent:applicationName];
|
||||
return [[self MR_directory:NSApplicationSupportDirectory] stringByAppendingPathComponent:applicationName];
|
||||
|
||||
#else
|
||||
|
||||
return [self directory:NSLibraryDirectory];
|
||||
return [self MR_directory:NSLibraryDirectory];
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
+ (NSURL *) urlForStoreName:(NSString *)storeFileName
|
||||
+ (NSURL *) MR_urlForStoreName:(NSString *)storeFileName
|
||||
{
|
||||
NSArray *paths = [NSArray arrayWithObjects:[self applicationDocumentsDirectory], [self applicationLibraryDirectory], nil];
|
||||
NSArray *paths = [NSArray arrayWithObjects:[self MR_applicationDocumentsDirectory], [self MR_applicationLibraryDirectory], nil];
|
||||
NSFileManager *fm = [[NSFileManager alloc] init];
|
||||
|
||||
for (NSString *path in paths)
|
||||
@@ -63,12 +63,12 @@ static NSPersistentStore *defaultPersistentStore = nil;
|
||||
}
|
||||
|
||||
//set default url
|
||||
return [NSURL fileURLWithPath:[[self applicationLibraryDirectory] stringByAppendingPathComponent:storeFileName]];
|
||||
return [NSURL fileURLWithPath:[[self MR_applicationLibraryDirectory] stringByAppendingPathComponent:storeFileName]];
|
||||
}
|
||||
|
||||
+ (NSURL *)defaultLocalStoreUrl
|
||||
+ (NSURL *)MR_defaultLocalStoreUrl
|
||||
{
|
||||
return [self urlForStoreName:kMagicalRecordDefaultStoreFileName];
|
||||
return [self MR_urlForStoreName:kMagicalRecordDefaultStoreFileName];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -11,18 +11,18 @@
|
||||
|
||||
@interface NSPersistentStoreCoordinator (MagicalRecord)
|
||||
|
||||
+ (NSPersistentStoreCoordinator *) defaultStoreCoordinator;
|
||||
+ (void) setDefaultStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator;
|
||||
+ (NSPersistentStoreCoordinator *) MR_defaultStoreCoordinator;
|
||||
+ (void) MR_setDefaultStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator;
|
||||
|
||||
+ (NSPersistentStoreCoordinator *) coordinatorWithInMemoryStore;
|
||||
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithInMemoryStore;
|
||||
|
||||
+ (NSPersistentStoreCoordinator *)newPersistentStoreCoordinator;
|
||||
+ (NSPersistentStoreCoordinator *) MR_newPersistentStoreCoordinator;
|
||||
|
||||
+ (NSPersistentStoreCoordinator *) coordinatorWithSqliteStoreNamed:(NSString *)storeFileName;
|
||||
+ (NSPersistentStoreCoordinator *) coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *) storeFileName;
|
||||
+ (NSPersistentStoreCoordinator *) coordinatorWithPersitentStore:(NSPersistentStore *)persistentStore;
|
||||
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString *)storeFileName;
|
||||
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *) storeFileName;
|
||||
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithPersitentStore:(NSPersistentStore *)persistentStore;
|
||||
|
||||
- (NSPersistentStore *) addInMemoryStore;
|
||||
- (NSPersistentStore *) MR_addInMemoryStore;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -15,24 +15,24 @@ static NSPersistentStoreCoordinator *defaultCoordinator = nil;
|
||||
|
||||
@implementation NSPersistentStoreCoordinator (MagicalRecord)
|
||||
|
||||
+ (NSPersistentStoreCoordinator *) defaultStoreCoordinator
|
||||
+ (NSPersistentStoreCoordinator *) MR_defaultStoreCoordinator
|
||||
{
|
||||
@synchronized (self)
|
||||
{
|
||||
if (defaultCoordinator == nil)
|
||||
{
|
||||
defaultCoordinator = [self newPersistentStoreCoordinator];
|
||||
defaultCoordinator = [self MR_newPersistentStoreCoordinator];
|
||||
}
|
||||
}
|
||||
return defaultCoordinator;
|
||||
}
|
||||
|
||||
+ (void) setDefaultStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator
|
||||
+ (void) MR_setDefaultStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator
|
||||
{
|
||||
defaultCoordinator = coordinator;
|
||||
}
|
||||
|
||||
- (void) setupSqliteStoreNamed:(id)storeFileName withOptions:(NSDictionary *)options
|
||||
- (void) MR_setupSqliteStoreNamed:(id)storeFileName withOptions:(NSDictionary *)options
|
||||
{
|
||||
NSURL *url = [storeFileName isKindOfClass:[NSURL class]] ? storeFileName : [NSPersistentStore urlForStoreName:storeFileName];
|
||||
NSError *error = nil;
|
||||
@@ -48,66 +48,66 @@ static NSPersistentStoreCoordinator *defaultCoordinator = nil;
|
||||
[NSPersistentStore setDefaultPersistentStore:store];
|
||||
}
|
||||
|
||||
+ (NSPersistentStoreCoordinator *) coordinatorWithPersitentStore:(NSPersistentStore *)persistentStore;
|
||||
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithPersitentStore:(NSPersistentStore *)persistentStore;
|
||||
{
|
||||
NSManagedObjectModel *model = [NSManagedObjectModel defaultManagedObjectModel];
|
||||
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
|
||||
|
||||
[psc setupSqliteStoreNamed:[persistentStore URL] withOptions:nil];
|
||||
[psc MR_setupSqliteStoreNamed:[persistentStore URL] withOptions:nil];
|
||||
|
||||
return psc;
|
||||
}
|
||||
|
||||
+ (NSPersistentStoreCoordinator *) coordinatorWithSqliteStoreNamed:(NSString *)storeFileName withOptions:(NSDictionary *)options
|
||||
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString *)storeFileName withOptions:(NSDictionary *)options
|
||||
{
|
||||
NSManagedObjectModel *model = [NSManagedObjectModel defaultManagedObjectModel];
|
||||
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
|
||||
|
||||
[psc setupSqliteStoreNamed:storeFileName withOptions:options];
|
||||
[psc MR_setupSqliteStoreNamed:storeFileName withOptions:options];
|
||||
|
||||
return psc;
|
||||
}
|
||||
|
||||
+ (NSPersistentStoreCoordinator *) coordinatorWithSqliteStoreNamed:(NSString *)storeFileName
|
||||
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString *)storeFileName
|
||||
{
|
||||
return [self coordinatorWithSqliteStoreNamed:storeFileName withOptions:nil];
|
||||
return [self MR_coordinatorWithSqliteStoreNamed:storeFileName withOptions:nil];
|
||||
}
|
||||
|
||||
- (void) setupAutoMigratingSqliteStoreNamed:(NSString *) storeFileName
|
||||
- (void) MR_setupAutoMigratingSqliteStoreNamed:(NSString *) storeFileName
|
||||
{
|
||||
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
|
||||
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
|
||||
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
|
||||
nil];
|
||||
|
||||
[self setupSqliteStoreNamed:storeFileName withOptions:options];
|
||||
[self MR_setupSqliteStoreNamed:storeFileName withOptions:options];
|
||||
}
|
||||
|
||||
+ (NSPersistentStoreCoordinator *) coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *) storeFileName
|
||||
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *) storeFileName
|
||||
{
|
||||
NSManagedObjectModel *model = [NSManagedObjectModel defaultManagedObjectModel];
|
||||
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
|
||||
|
||||
[coordinator setupAutoMigratingSqliteStoreNamed:storeFileName];
|
||||
[coordinator MR_setupAutoMigratingSqliteStoreNamed:storeFileName];
|
||||
|
||||
//HACK: lame solution to fix automigration error "Migration failed after first pass"
|
||||
if ([[coordinator persistentStores] count] == 0)
|
||||
{
|
||||
[coordinator performSelector:@selector(setupAutoMigratingSqliteStoreNamed:) withObject:storeFileName afterDelay:0.5];
|
||||
[coordinator performSelector:@selector(MR_setupAutoMigratingSqliteStoreNamed:) withObject:storeFileName afterDelay:0.5];
|
||||
}
|
||||
return coordinator;
|
||||
}
|
||||
|
||||
+ (NSPersistentStoreCoordinator *) coordinatorWithInMemoryStore
|
||||
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithInMemoryStore
|
||||
{
|
||||
NSManagedObjectModel *model = [NSManagedObjectModel defaultManagedObjectModel];
|
||||
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
|
||||
|
||||
[NSPersistentStore setDefaultPersistentStore:[psc addInMemoryStore]];
|
||||
[NSPersistentStore setDefaultPersistentStore:[psc MR_addInMemoryStore]];
|
||||
return psc;
|
||||
}
|
||||
|
||||
- (NSPersistentStore *) addInMemoryStore
|
||||
- (NSPersistentStore *) MR_addInMemoryStore
|
||||
{
|
||||
NSError *error = nil;
|
||||
NSPersistentStore *store = [self addPersistentStoreWithType:NSInMemoryStoreType
|
||||
@@ -122,9 +122,9 @@ static NSPersistentStoreCoordinator *defaultCoordinator = nil;
|
||||
return store;
|
||||
}
|
||||
|
||||
+ (NSPersistentStoreCoordinator *) newPersistentStoreCoordinator
|
||||
+ (NSPersistentStoreCoordinator *) MR_newPersistentStoreCoordinator
|
||||
{
|
||||
return [self coordinatorWithSqliteStoreNamed:kMagicalRecordDefaultStoreFileName];
|
||||
return [self MR_coordinatorWithSqliteStoreNamed:kMagicalRecordDefaultStoreFileName];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -17,7 +17,7 @@ static SEL errorHandlerAction = nil;
|
||||
[MRCoreDataAction cleanUp];
|
||||
[NSManagedObjectContext setDefaultContext:nil];
|
||||
[NSManagedObjectModel setDefaultManagedObjectModel:nil];
|
||||
[NSPersistentStoreCoordinator setDefaultStoreCoordinator:nil];
|
||||
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:nil];
|
||||
[NSPersistentStore setDefaultPersistentStore:nil];
|
||||
}
|
||||
|
||||
@@ -100,8 +100,8 @@ static SEL errorHandlerAction = nil;
|
||||
|
||||
+ (void) setupCoreDataStackWithStoreNamed:(NSString *)storeName
|
||||
{
|
||||
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator coordinatorWithSqliteStoreNamed:storeName];
|
||||
[NSPersistentStoreCoordinator setDefaultStoreCoordinator:coordinator];
|
||||
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithSqliteStoreNamed:storeName];
|
||||
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator];
|
||||
|
||||
NSManagedObjectContext *context = [NSManagedObjectContext contextWithStoreCoordinator:coordinator];
|
||||
[NSManagedObjectContext setDefaultContext:context];
|
||||
@@ -109,8 +109,8 @@ static SEL errorHandlerAction = nil;
|
||||
|
||||
+ (void) setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(NSString *)storeName
|
||||
{
|
||||
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator coordinatorWithAutoMigratingSqliteStoreNamed:storeName];
|
||||
[NSPersistentStoreCoordinator setDefaultStoreCoordinator:coordinator];
|
||||
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithAutoMigratingSqliteStoreNamed:storeName];
|
||||
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator];
|
||||
|
||||
NSManagedObjectContext *context = [NSManagedObjectContext contextWithStoreCoordinator:coordinator];
|
||||
[NSManagedObjectContext setDefaultContext:context];
|
||||
@@ -118,8 +118,8 @@ static SEL errorHandlerAction = nil;
|
||||
|
||||
+ (void) setupCoreDataStackWithInMemoryStore
|
||||
{
|
||||
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator coordinatorWithInMemoryStore];
|
||||
[NSPersistentStoreCoordinator setDefaultStoreCoordinator:coordinator];
|
||||
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithInMemoryStore];
|
||||
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator];
|
||||
|
||||
NSManagedObjectContext *context = [NSManagedObjectContext contextWithStoreCoordinator:coordinator];
|
||||
[NSManagedObjectContext setDefaultContext:context];
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
{
|
||||
assertThat([NSManagedObjectContext defaultContext], is(notNilValue()));
|
||||
assertThat([NSManagedObjectModel defaultManagedObjectModel], is(notNilValue()));
|
||||
assertThat([NSPersistentStoreCoordinator defaultStoreCoordinator], is(notNilValue()));
|
||||
assertThat([NSPersistentStoreCoordinator MR_defaultStoreCoordinator], is(notNilValue()));
|
||||
assertThat([NSPersistentStore defaultPersistentStore], is(notNilValue()));
|
||||
}
|
||||
|
||||
@@ -90,7 +90,6 @@
|
||||
assertThatBool(errorHandlerWasCalled_, is(equalToBool(YES)));
|
||||
}
|
||||
|
||||
|
||||
- (void) testLogsErrorsToLogger
|
||||
{
|
||||
GHFail(@"Test Not Implemented");
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#define HC_SHORTHAND
|
||||
#import <OCHamcrestIOS/OCHamcrestIOS.h>
|
||||
|
||||
#define MR_SHORTHAND
|
||||
#import "CoreData+MagicalRecord.h"
|
||||
#import "FixtureHelpers.h"
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user