Skip to content

Commit 4561e0f

Browse files
committed
Fixed Core Data test cases problem in multithreading condition.
1 parent 6d1d1bb commit 4561e0f

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed

MJExtension.xcodeproj/xcshareddata/xcschemes/MJExtensionDemo.xcscheme

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,33 @@
6060
ReferencedContainer = "container:MJExtension.xcodeproj">
6161
</BuildableReference>
6262
</BuildableProductRunnable>
63+
<CommandLineArguments>
64+
<CommandLineArgument
65+
argument = "-com.apple.CoreData.SQLDebug 4"
66+
isEnabled = "YES">
67+
</CommandLineArgument>
68+
<CommandLineArgument
69+
argument = "-com.apple.CoreData.ConcurrencyDebug 1"
70+
isEnabled = "YES">
71+
</CommandLineArgument>
72+
</CommandLineArguments>
73+
<EnvironmentVariables>
74+
<EnvironmentVariable
75+
key = "SQLITE_AUTO_TRACE"
76+
value = "1"
77+
isEnabled = "NO">
78+
</EnvironmentVariable>
79+
<EnvironmentVariable
80+
key = "SQLITE_ENABLE_FILE_ASSERSIONS"
81+
value = "1"
82+
isEnabled = "YES">
83+
</EnvironmentVariable>
84+
<EnvironmentVariable
85+
key = "SQLITE_ENABLE_THREAD_ASSERTIONS"
86+
value = "1"
87+
isEnabled = "YES">
88+
</EnvironmentVariable>
89+
</EnvironmentVariables>
6390
</LaunchAction>
6491
<ProfileAction
6592
buildConfiguration = "Release"

MJExtensionTests/CoreDataTests.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ class CoreDataTests: XCTestCase {
1717
try super.setUpWithError()
1818

1919
let container = NSPersistentContainer(name: "MJCoreDataTestModel")
20-
let description = NSPersistentStoreDescription()
21-
description.url = URL(fileURLWithPath: "/dev/null")
22-
23-
container.persistentStoreDescriptions = [description]
20+
// Test in memory. This is important.
21+
container.persistentStoreDescriptions.first?.url = URL(fileURLWithPath: "/dev/null")
2422
container.loadPersistentStores { description, error in
2523
XCTAssertNil(error)
2624
}
@@ -56,19 +54,22 @@ class CoreDataTests: XCTestCase {
5654
coreDataObject2JSON()
5755
}
5856

59-
func json2CoreDataObject() {
57+
func json2CoreDataObject() {
58+
context.performAndWait {
6059
guard let tester = MJCoreDataTester.mj_object(withKeyValues: Values.testJSONObject, context: context) else {
6160
XCTAssert(false, "conversion to core data object failed")
6261
return
6362
}
64-
63+
6564
XCTAssert(tester.isJuan == Values.isJuan)
6665
XCTAssert(tester.identifier == Values.identifier)
6766
XCTAssert(tester.name == Values.name)
6867
XCTAssert(tester.age == Values.age)
6968
}
70-
71-
func coreDataObject2JSON() {
69+
}
70+
71+
func coreDataObject2JSON() {
72+
context.performAndWait {
7273
let coreDataObject = NSEntityDescription.insertNewObject(forEntityName: MJCoreDataTester.entity().name!, into: context) as! MJCoreDataTester
7374
coreDataObject.name = Values.name
7475
coreDataObject.age = Int16(Values.age)
@@ -85,5 +86,6 @@ class CoreDataTests: XCTestCase {
8586
XCTAssert(dict["name"] as? String == Values.name)
8687
XCTAssert(dict["age"] as! Int == Values.age)
8788
}
89+
}
8890

8991
}

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:5.1
22

33
import PackageDescription
44

README.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -462,22 +462,20 @@ NSLog(@"%@", dictArray);
462462
463463
### <a id="Core_Data"></a> Core Data
464464
465-
```objc
466-
NSDictionary *dict = @{
467-
@"name" : @"Jack",
468-
@"icon" : @"lufy.png",
469-
@"age" : @20,
470-
@"height" : @1.55,
471-
@"money" : @"100.9",
472-
@"sex" : @(SexFemale),
473-
@"gay" : @"true"
474-
};
475-
476-
// This demo just provide simple steps
477-
NSManagedObjectContext *context = nil;
478-
User *user = [User mj_objectWithKeyValues:dict context:context];
479-
480-
[context save:nil];
465+
```swift
466+
func json2CoreDataObject() {
467+
context.performAndWait {
468+
let object = MJCoreDataTester.mj_object(withKeyValues: Values.testJSONObject, context: context)
469+
// use the object
470+
}
471+
}
472+
473+
func coreDataObject2JSON() {
474+
context.performAndWait {
475+
let dict = coreDataObject.mj_keyValues()
476+
// use dict
477+
}
478+
}
481479
```
482480

483481
### <a id="Coding"></a> Coding (Archive & Unarchive methods are deprecated in iOS 12)

0 commit comments

Comments
 (0)