Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #58 from 3lvis/feature/add-method-to-save-backgrou…
Browse files Browse the repository at this point in the history
…nd-context-without-saving

Add method to save background context without merging with main context
  • Loading branch information
3lvis committed May 15, 2016
2 parents a2adcf0 + 50d4a68 commit 4d986c9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 57 deletions.
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
osx_image: xcode7.3
language: objective-c
cache: cocoapods
before_install: gem install xcpretty cocoapods obcd slather -N
podfile: Podfile
script: xcodebuild -workspace Demo.xcworkspace -scheme Tests -sdk iphonesimulator build test -destination 'platform=iOS Simulator,name=iPhone 6' GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES GCC_GENERATE_TEST_COVERAGE_FILES=YES clean test && exit ${PIPESTATUS[0]}
after_success: slather
podfile: Travis/Podfile
before_install: gem install xcpretty
script: xcodebuild -workspace Demo.xcworkspace -scheme Tests -sdk iphonesimulator build test -destination 'platform=iOS Simulator,name=iPhone 6' GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES GCC_GENERATE_TEST_COVERAGE_FILES=YES clean test | xcpretty -c
notifications:
email: false
50 changes: 0 additions & 50 deletions Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
14EEAC3C1CBFADA1002E495A /* TestCheck.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14BC05391CBEF21C006278EA /* TestCheck.swift */; };
14EEAC3D1CBFADD6002E495A /* DATAStack.h in Headers */ = {isa = PBXBuildFile; fileRef = 14B3898D1CBF8166001123DC /* DATAStack.h */; settings = {ATTRIBUTES = (Public, ); }; };
9D8B2A9BEA8134EADD5A035E /* Pods_CocoaPods_DemoSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 456313516DE511EF34E737F8 /* Pods_CocoaPods_DemoSwift.framework */; };
AAD5F3FB4EF0132D1EA823A4 /* Pods_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 361F47EB6A4E097F4F9ECCD0 /* Pods_Tests.framework */; };
DFED6C60B0F6959CB9404D01 /* Pods_CocoaPods_DemoObjectiveC.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 66CD307C7D7A36353574E32B /* Pods_CocoaPods_DemoObjectiveC.framework */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -110,7 +109,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
AAD5F3FB4EF0132D1EA823A4 /* Pods_Tests.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -336,12 +334,9 @@
isa = PBXNativeTarget;
buildConfigurationList = 146D72B91AB782920058798C /* Build configuration list for PBXNativeTarget "Tests" */;
buildPhases = (
702054E931FBBEE2FF37C842 /* 📦 Check Pods Manifest.lock */,
146D72A81AB782920058798C /* Sources */,
146D72A91AB782920058798C /* Frameworks */,
146D72AA1AB782920058798C /* Resources */,
2F59D22415DB122C07AEBD58 /* 📦 Embed Pods Frameworks */,
2804D6AD685E22B0B39CD3EC /* 📦 Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -595,21 +590,6 @@
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-CocoaPods-DemoObjectiveC/Pods-CocoaPods-DemoObjectiveC-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
2804D6AD685E22B0B39CD3EC /* 📦 Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Tests/Pods-Tests-resources.sh\"\n";
showEnvVarsInLog = 0;
};
2C333B6809CE68E39177C127 /* 📦 Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand All @@ -625,21 +605,6 @@
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-CocoaPods-DemoObjectiveC/Pods-CocoaPods-DemoObjectiveC-resources.sh\"\n";
showEnvVarsInLog = 0;
};
2F59D22415DB122C07AEBD58 /* 📦 Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Tests/Pods-Tests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
310090EE9964B419CFB4A8ED /* 📦 Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down Expand Up @@ -670,21 +635,6 @@
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-CocoaPods-DemoSwift/Pods-CocoaPods-DemoSwift-resources.sh\"\n";
showEnvVarsInLog = 0;
};
702054E931FBBEE2FF37C842 /* 📦 Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Check Pods Manifest.lock";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
C476F78B7F2169F358088B7F /* 📦 Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down
12 changes: 12 additions & 0 deletions Source/DATAStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ import CoreData
context.performSelector(DATAStack.performSelectorForBackgroundContext(), withObject: blockObject)
}

/**
Most of the time when saving a background context you want that it merges the saved changes with the main
context, but when you're performing partial savings, such as batch saves you don't want this to be the case.
- parameter context: The block that contains the created background context.
*/
public func saveBackgroundContextWithoutMergingWithMainContext(context: NSManagedObjectContext) throws {
assert(context.concurrencyType == DATAStack.backgroundConcurrencyType())
NSNotificationCenter.defaultCenter().removeObserver(self, name: NSManagedObjectContextDidSaveNotification, object: context)
try context.save()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(DATAStack.backgroundContextDidSave(_:)), name: NSManagedObjectContextDidSaveNotification, object: context)
}

func saveMainThread(completion: ((error: NSError?) -> Void)?) {
var writerContextError: NSError?
let writerContextBlock: @convention(block) Void -> Void = {
Expand Down
8 changes: 6 additions & 2 deletions Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,19 @@ class Tests: XCTestCase {
let objectsA = self.fetchObjectsInContext(dataStack.mainContext)
XCTAssertEqual(objectsA.count, 1)

try! dataStack.drop()
let _ = try? dataStack.drop()

let objects = self.fetchObjectsInContext(dataStack.mainContext)
XCTAssertEqual(objects.count, 0)
}

func testAlternativeModel() {
let dataStack = DATAStack(modelName: "DataModelTest", bundle: NSBundle(forClass: Tests.self), storeType: .SQLite)
let dataStack = DATAStack(modelName: "DataModelTest", bundle: NSBundle(forClass: Tests.self), storeType: .InMemory)
self.insertUserInContext(dataStack.mainContext)

let objects = self.fetchObjectsInContext(dataStack.mainContext)
XCTAssertEqual(objects.count, 1)

XCTAssertNotNil(dataStack)
}
}
1 change: 1 addition & 0 deletions Travis/Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xcodeproj '../Demo.xcodeproj'

0 comments on commit 4d986c9

Please sign in to comment.