Skip to content

Commit

Permalink
Merge pull request #52 from designatednerd/master
Browse files Browse the repository at this point in the history
Custom bundles
  • Loading branch information
designatednerd committed Oct 20, 2015
2 parents 515b52a + c220efe commit 7571b39
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 28 deletions.
16 changes: 14 additions & 2 deletions Pod/Classes/VOKCoreDataManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,26 @@ typedef void(^VOKObjectIDsReturnBlock)(NSArray *arrayOfManagedObjectIDs);
- (NSManagedObjectModel *)managedObjectModel;

/**
Set the name of the managed object model and the name of the SQL lite store on disk. Call this first when you setup the core data stack.
Set the name of the managed object model and the name of the SQL lite store on disk. Call this first when you setup the core data stack.
The main context will be initialized immediately. This method should only be called from the main queue.
@param resource The filename of the mom or momd file in your project. If nil the first model found in the main bundle will be used.
NOTE: This assumes your managed object file is in the main bundle. If it isn't, use setResource:database:bundle: instead.
@param resource The filename of the mom or momd file in your project. If nil the first model found in this class's bundle will be used.
@param database The filename of the SQLite store in your application. A nil database name will create an in-memory store.
*/
- (void)setResource:(NSString *)resource
database:(NSString *)database;

/**
Set the name of the managed object model and the name of the SQL lite store on disk, in the provided bundle. Call this first when you setup the core data stack.
The main context will be initialized immediately. This method should only be called from the main queue.
@param resource The filename of the mom or momd file in your project. If nil the first model found in the provided bundle will be used.
@param database The filename of the SQLite store in your application. A nil database name will create an in-memory store.
@param bundle The bundle where the Managed Object Model can be found. If nil, the main bundle will be used.
*/
- (void)setResource:(NSString *)resource
database:(NSString *)database
bundle:(NSBundle *)bundle;

/**
In case of a migration failure, these options allow possible recovery and notification
*/
Expand Down
38 changes: 33 additions & 5 deletions Pod/Classes/VOKCoreDataManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ @interface VOKCoreDataManager () {
@property (nonatomic, copy) NSString *resource;
@property (nonatomic, copy) NSString *databaseFilename;
@property (nonatomic, strong) NSMutableDictionary *mapperCollection;
@property (nonatomic, strong) NSBundle *bundleForModel;

@end

Expand Down Expand Up @@ -62,9 +63,23 @@ - (instancetype)init
}

- (void)setResource:(NSString *)resource database:(NSString *)database
{
[self setResource:resource
database:database
bundle:nil];
}

- (void)setResource:(NSString *)resource
database:(NSString *)database
bundle:(NSBundle *)bundle
{
self.resource = resource;
self.databaseFilename = database;

if (bundle) {
self.bundleForModel = bundle;
}

[[VOKCoreDataManager sharedInstance] managedObjectContext];
}

Expand Down Expand Up @@ -112,13 +127,23 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
return _persistentStoreCoordinator;
}

- (NSBundle *)bundleForModel
{
if (!_bundleForModel) {
//Default to using the main bundle
_bundleForModel = [NSBundle mainBundle];
}

return _bundleForModel;
}

#pragma mark - Initializers

- (void)initManagedObjectModel
{
NSURL *modelURL = [[NSBundle bundleForClass:[self class]] URLForResource:self.resource withExtension:@"momd"];
NSURL *modelURL = [self.bundleForModel URLForResource:self.resource withExtension:@"momd"];
if (!modelURL) {
modelURL = [[NSBundle bundleForClass:[self class]] URLForResource:self.resource withExtension:@"mom"];
modelURL = [self.bundleForModel URLForResource:self.resource withExtension:@"mom"];
}
NSAssert(modelURL, @"Managed object model not found.");
if (modelURL) {
Expand Down Expand Up @@ -594,18 +619,21 @@ - (NSURL *)applicationLibraryDirectory

- (void)resetCoreData
{
NSArray *stores = [[self persistentStoreCoordinator] persistentStores];

//Use the instance variable so as not to accidentally spin up a new instance if
//one does not already exist.
NSArray *stores = [_persistentStoreCoordinator persistentStores];

for (NSPersistentStore *store in stores) {
[[self persistentStoreCoordinator] removePersistentStore:store error:nil];
if (self.databaseFilename) {
[[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:nil];
[[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:nil];
}
}

_persistentStoreCoordinator = nil;
_managedObjectContext = nil;
_managedObjectModel = nil;
_bundleForModel = nil;
[_mapperCollection removeAllObjects];
[self addMappableModelMappers];
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions SampleProject/SampleProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
0592A962B2406AA43A09FF8B /* libPods-VOKCoreDataManager.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 92D82C6CD9409B2DDCC878AA /* libPods-VOKCoreDataManager.a */; };
3393A9ED1BD6B85200F43B45 /* BundleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3393A9EC1BD6B85200F43B45 /* BundleTests.m */; settings = {ASSET_TAGS = (); }; };
3D40DE3D180729A10048D5EE /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3D40DE34180728BF0048D5EE /* XCTest.framework */; };
3D40DE3E180729A10048D5EE /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EA5F19A615C196500089AA44 /* Foundation.framework */; };
3D40DE3F180729A10048D5EE /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EA5F19A415C196500089AA44 /* UIKit.framework */; };
Expand All @@ -25,7 +26,6 @@
71E1AB061A53423D004311E5 /* ManagedObjectImportExportTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 71E1AAF71A5340F3004311E5 /* ManagedObjectImportExportTests.m */; };
71E1AB091A534274004311E5 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 71E1AB071A534274004311E5 /* InfoPlist.strings */; };
71E1AB0D1A5342E9004311E5 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 71E1AB0A1A5342E5004311E5 /* InfoPlist.strings */; };
71E1AB0E1A534468004311E5 /* VICoreDataModel.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 71E1AAE41A5340A1004311E5 /* VICoreDataModel.xcdatamodeld */; };
8434D7401A93F44E005A9B2F /* VOKManagedObjectAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 8434D73F1A93F44E005A9B2F /* VOKManagedObjectAdditions.m */; };
EA5F19A515C196500089AA44 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EA5F19A415C196500089AA44 /* UIKit.framework */; };
EA5F19A715C196500089AA44 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EA5F19A615C196500089AA44 /* Foundation.framework */; };
Expand All @@ -45,6 +45,7 @@

/* Begin PBXFileReference section */
2D50205FA97F8F44658BA488 /* Pods-VOKCoreDataManager Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-VOKCoreDataManager Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-VOKCoreDataManager Tests/Pods-VOKCoreDataManager Tests.release.xcconfig"; sourceTree = "<group>"; };
3393A9EC1BD6B85200F43B45 /* BundleTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BundleTests.m; path = VOKCoreDataManagerTests/BundleTests.m; sourceTree = SOURCE_ROOT; };
3525B1DF80B2F57050AE6E85 /* Pods-VOKCoreDataManager.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-VOKCoreDataManager.release.xcconfig"; path = "Pods/Target Support Files/Pods-VOKCoreDataManager/Pods-VOKCoreDataManager.release.xcconfig"; sourceTree = "<group>"; };
3D40DE34180728BF0048D5EE /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
3D40DE3C180729A10048D5EE /* VOKCoreDataManager Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "VOKCoreDataManager Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -116,6 +117,7 @@
3D40DE40180729A10048D5EE /* VOKCoreDataManagerTests */ = {
isa = PBXGroup;
children = (
3393A9EC1BD6B85200F43B45 /* BundleTests.m */,
71E1AAF51A5340ED004311E5 /* CoreDataManagerDeleteTests.m */,
71E1AAF71A5340F3004311E5 /* ManagedObjectImportExportTests.m */,
8434D73F1A93F44E005A9B2F /* VOKManagedObjectAdditions.m */,
Expand Down Expand Up @@ -291,7 +293,7 @@
isa = PBXProject;
attributes = {
CLASSPREFIX = VOK;
LastUpgradeCheck = 0630;
LastUpgradeCheck = 0700;
TargetAttributes = {
3D40DE3B180729A10048D5EE = {
TestTargetID = EA5F199F15C196500089AA44;
Expand Down Expand Up @@ -418,9 +420,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3393A9ED1BD6B85200F43B45 /* BundleTests.m in Sources */,
71E1AB061A53423D004311E5 /* ManagedObjectImportExportTests.m in Sources */,
8434D7401A93F44E005A9B2F /* VOKManagedObjectAdditions.m in Sources */,
71E1AB0E1A534468004311E5 /* VICoreDataModel.xcdatamodeld in Sources */,
71E1AB051A534239004311E5 /* CoreDataManagerDeleteTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -501,6 +503,7 @@
INFOPLIST_FILE = "VOKCoreDataManagerTests/VOKCoreDataManager Tests-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.vokal.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "VOKCoreDataManager Tests";
TEST_HOST = "$(BUNDLE_LOADER)";
WRAPPER_EXTENSION = xctest;
Expand Down Expand Up @@ -534,6 +537,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
INFOPLIST_FILE = "VOKCoreDataManagerTests/VOKCoreDataManager Tests-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.vokal.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "VOKCoreDataManager Tests";
TEST_HOST = "$(BUNDLE_LOADER)";
WRAPPER_EXTENSION = xctest;
Expand All @@ -556,6 +560,7 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = "compiler-default";
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -619,6 +624,7 @@
GCC_PREFIX_HEADER = "Supporting Files/VOKCoreDataManager-Prefix.pch";
INFOPLIST_FILE = "$(SRCROOT)/Supporting Files/VOKCoreDataManager-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.VOKAL.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = VOKCoreDataManager;
WRAPPER_EXTENSION = app;
};
Expand All @@ -634,6 +640,7 @@
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
INFOPLIST_FILE = "$(SRCROOT)/Supporting Files/VOKCoreDataManager-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.VOKAL.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = VOKCoreDataManager;
WRAPPER_EXTENSION = app;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
LastUpgradeVersion = "0700"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -23,10 +23,10 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -48,15 +48,18 @@
ReferencedContainer = "container:SampleProject.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand All @@ -72,10 +75,10 @@
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>com.VOKAL.${PRODUCT_NAME:rfc1034identifier}</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
Loading

0 comments on commit 7571b39

Please sign in to comment.