Skip to content

Commit 78f9876

Browse files
committed
Add a new ManagedObjectContextFactory backed by persistent container
1 parent d072160 commit 78f9876

3 files changed

Lines changed: 45 additions & 8 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import ObjectiveC
2+
3+
@objc
4+
class ContainerContextFactory: NSObject, ManagedObjectContextFactory {
5+
6+
private let container: NSPersistentContainer
7+
8+
let mainContext: NSManagedObjectContext
9+
10+
required init(persistentContainer container: NSPersistentContainer) {
11+
self.container = container
12+
self.mainContext = container.viewContext
13+
self.mainContext.automaticallyMergesChangesFromParent = true
14+
self.mainContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
15+
}
16+
17+
func newDerivedContext() -> NSManagedObjectContext {
18+
let context = container.newBackgroundContext()
19+
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
20+
return context
21+
}
22+
23+
func save(_ context: NSManagedObjectContext, andWait wait: Bool, withCompletionBlock completionBlock: (() -> Void)?) {
24+
let block: () -> Void = {
25+
self.internalSave(context)
26+
DispatchQueue.main.async {
27+
completionBlock?()
28+
}
29+
}
30+
if (wait) {
31+
context.performAndWait(block)
32+
} else {
33+
context.perform(block)
34+
}
35+
}
36+
37+
}

WordPress/WordPress.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,8 @@
980980
4A266B91282B13A70089CF3D /* CoreDataTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A266B90282B13A70089CF3D /* CoreDataTestCase.swift */; };
981981
4A50667C28B3216500DD09F4 /* LegacyContextFactory.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A50667B28B3216500DD09F4 /* LegacyContextFactory.m */; };
982982
4A50667D28B3216500DD09F4 /* LegacyContextFactory.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A50667B28B3216500DD09F4 /* LegacyContextFactory.m */; };
983+
4A50668028B364CA00DD09F4 /* ContainerContextFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A50667F28B364CA00DD09F4 /* ContainerContextFactory.swift */; };
984+
4A50668128B364CA00DD09F4 /* ContainerContextFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A50667F28B364CA00DD09F4 /* ContainerContextFactory.swift */; };
983985
4AFB8FBF2824999500A2F4B2 /* ContextManagerMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AFB8FBE2824999400A2F4B2 /* ContextManagerMock.swift */; };
984986
4B2DD0F29CD6AC353C056D41 /* Pods_WordPressUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8DCE7542239FBC709B90EA85 /* Pods_WordPressUITests.framework */; };
985987
4C8A715EBCE7E73AEE216293 /* Pods_WordPressShareExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F47DB4A8EC2E6844E213A3FA /* Pods_WordPressShareExtension.framework */; };
@@ -5915,6 +5917,7 @@
59155917
4A50667A28B3216500DD09F4 /* LegacyContextFactory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LegacyContextFactory.h; sourceTree = "<group>"; };
59165918
4A50667B28B3216500DD09F4 /* LegacyContextFactory.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LegacyContextFactory.m; sourceTree = "<group>"; };
59175919
4A50667E28B3218800DD09F4 /* ManagedObjectContextFactory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ManagedObjectContextFactory.h; sourceTree = "<group>"; };
5920+
4A50667F28B364CA00DD09F4 /* ContainerContextFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContainerContextFactory.swift; sourceTree = "<group>"; };
59185921
4AFB8FBE2824999400A2F4B2 /* ContextManagerMock.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContextManagerMock.swift; sourceTree = "<group>"; };
59195922
4D520D4E22972BC9002F5924 /* acknowledgements.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = acknowledgements.html; path = "../Pods/Target Support Files/Pods-Apps-WordPress/acknowledgements.html"; sourceTree = "<group>"; };
59205923
51A5F017948878F7E26979A0 /* Pods-Apps-WordPress.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Apps-WordPress.release.xcconfig"; path = "../Pods/Target Support Files/Pods-Apps-WordPress/Pods-Apps-WordPress.release.xcconfig"; sourceTree = "<group>"; };
@@ -13856,6 +13859,7 @@
1385613859
4A50667E28B3218800DD09F4 /* ManagedObjectContextFactory.h */,
1385713860
4A50667A28B3216500DD09F4 /* LegacyContextFactory.h */,
1385813861
4A50667B28B3216500DD09F4 /* LegacyContextFactory.m */,
13862+
4A50667F28B364CA00DD09F4 /* ContainerContextFactory.swift */,
1385913863
E1E5EE36231E47A80018E9E3 /* ContextManager+ErrorHandling.swift */,
1386013864
B5ECA6C91DBAA0020062D7E0 /* CoreDataHelper.swift */,
1386113865
24F3789725E6E62100A27BB7 /* NSManagedObject+Lookup.swift */,
@@ -19204,6 +19208,7 @@
1920419208
17523381246C4F9200870B4A /* HomepageSettingsViewController.swift in Sources */,
1920519209
E62CE58E26B1D14200C9D147 /* AccountService+Cookies.swift in Sources */,
1920619210
C81CCD7F243BF7A600A83E27 /* TenorMediaGroup.swift in Sources */,
19211+
4A50668028B364CA00DD09F4 /* ContainerContextFactory.swift in Sources */,
1920719212
B0F2EFBF259378E600C7EB6D /* SiteSuggestionService.swift in Sources */,
1920819213
4388FF0020A4E19C00783948 /* NotificationsViewController+PushPrimer.swift in Sources */,
1920919214
98517E5928220411001FFD45 /* BloggingPromptTableViewCell.swift in Sources */,
@@ -22238,6 +22243,7 @@
2223822243
FABB25AE2602FC2C00C8785C /* SearchWrapperView.swift in Sources */,
2223922244
98830A932747043B0061A87C /* BorderedButtonTableViewCell.swift in Sources */,
2224022245
FABB25AF2602FC2C00C8785C /* UserSettings.swift in Sources */,
22246+
4A50668128B364CA00DD09F4 /* ContainerContextFactory.swift in Sources */,
2224122247
FABB25B02602FC2C00C8785C /* MediaImportService.swift in Sources */,
2224222248
FABB25B12602FC2C00C8785C /* NSAttributedStringKey+Conversion.swift in Sources */,
2224322249
FABB25B22602FC2C00C8785C /* SelectPostViewController.swift in Sources */,

WordPress/WordPressTest/NotificationSyncMediatorTests.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ class NotificationSyncMediatorTests: CoreDataTestCase {
5656
// Make sure the collection is empty, to begin with
5757
XCTAssert(mainContext.countObjects(ofType: Notification.self) == 0)
5858

59-
// CoreData Expectations
60-
let contextSaved = expectation(forNotification: .NSManagedObjectContextDidSave, object: mainContext)
61-
6259
// Mediator Expectations
6360
let expect = expectation(description: "Sync")
6461

@@ -68,7 +65,7 @@ class NotificationSyncMediatorTests: CoreDataTestCase {
6865
expect.fulfill()
6966
}
7067

71-
wait(for: [contextSaved, expect], timeout: timeout)
68+
wait(for: [expect], timeout: timeout)
7269
}
7370

7471

@@ -122,9 +119,6 @@ class NotificationSyncMediatorTests: CoreDataTestCase {
122119
// Make sure the collection is empty, to begin with
123120
XCTAssert(mainContext.countObjects(ofType: Notification.self) == 0)
124121

125-
// CoreData Expectations
126-
let contextSaved = expectation(forNotification: .NSManagedObjectContextDidSave, object: mainContext)
127-
128122
// Mediator Expectations
129123
let expect = expectation(description: "Sync")
130124

@@ -135,7 +129,7 @@ class NotificationSyncMediatorTests: CoreDataTestCase {
135129
expect.fulfill()
136130
}
137131

138-
wait(for: [contextSaved, expect], timeout: timeout)
132+
wait(for: [expect], timeout: timeout)
139133
}
140134

141135

0 commit comments

Comments
 (0)