Skip to content

Commit

Permalink
Fixed Core Data test cases problem in multithreading condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfcon committed Sep 17, 2021
1 parent 6d1d1bb commit 4561e0f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 25 deletions.
Expand Up @@ -60,6 +60,33 @@
ReferencedContainer = "container:MJExtension.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "-com.apple.CoreData.SQLDebug 4"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "-com.apple.CoreData.ConcurrencyDebug 1"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
<EnvironmentVariables>
<EnvironmentVariable
key = "SQLITE_AUTO_TRACE"
value = "1"
isEnabled = "NO">
</EnvironmentVariable>
<EnvironmentVariable
key = "SQLITE_ENABLE_FILE_ASSERSIONS"
value = "1"
isEnabled = "YES">
</EnvironmentVariable>
<EnvironmentVariable
key = "SQLITE_ENABLE_THREAD_ASSERTIONS"
value = "1"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
18 changes: 10 additions & 8 deletions MJExtensionTests/CoreDataTests.swift
Expand Up @@ -17,10 +17,8 @@ class CoreDataTests: XCTestCase {
try super.setUpWithError()

let container = NSPersistentContainer(name: "MJCoreDataTestModel")
let description = NSPersistentStoreDescription()
description.url = URL(fileURLWithPath: "/dev/null")

container.persistentStoreDescriptions = [description]
// Test in memory. This is important.
container.persistentStoreDescriptions.first?.url = URL(fileURLWithPath: "/dev/null")
container.loadPersistentStores { description, error in
XCTAssertNil(error)
}
Expand Down Expand Up @@ -56,19 +54,22 @@ class CoreDataTests: XCTestCase {
coreDataObject2JSON()
}

func json2CoreDataObject() {
func json2CoreDataObject() {
context.performAndWait {
guard let tester = MJCoreDataTester.mj_object(withKeyValues: Values.testJSONObject, context: context) else {
XCTAssert(false, "conversion to core data object failed")
return
}

XCTAssert(tester.isJuan == Values.isJuan)
XCTAssert(tester.identifier == Values.identifier)
XCTAssert(tester.name == Values.name)
XCTAssert(tester.age == Values.age)
}

func coreDataObject2JSON() {
}

func coreDataObject2JSON() {
context.performAndWait {
let coreDataObject = NSEntityDescription.insertNewObject(forEntityName: MJCoreDataTester.entity().name!, into: context) as! MJCoreDataTester
coreDataObject.name = Values.name
coreDataObject.age = Int16(Values.age)
Expand All @@ -85,5 +86,6 @@ class CoreDataTests: XCTestCase {
XCTAssert(dict["name"] as? String == Values.name)
XCTAssert(dict["age"] as! Int == Values.age)
}
}

}
2 changes: 1 addition & 1 deletion Package.swift
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:5.1

import PackageDescription

Expand Down
30 changes: 14 additions & 16 deletions README.md
Expand Up @@ -462,22 +462,20 @@ NSLog(@"%@", dictArray);
### <a id="Core_Data"></a> Core Data
```objc
NSDictionary *dict = @{
@"name" : @"Jack",
@"icon" : @"lufy.png",
@"age" : @20,
@"height" : @1.55,
@"money" : @"100.9",
@"sex" : @(SexFemale),
@"gay" : @"true"
};
// This demo just provide simple steps
NSManagedObjectContext *context = nil;
User *user = [User mj_objectWithKeyValues:dict context:context];
[context save:nil];
```swift
func json2CoreDataObject() {
context.performAndWait {
let object = MJCoreDataTester.mj_object(withKeyValues: Values.testJSONObject, context: context)
// use the object
}
}
func coreDataObject2JSON() {
context.performAndWait {
let dict = coreDataObject.mj_keyValues()
// use dict
}
}
```

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

0 comments on commit 4561e0f

Please sign in to comment.