Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/batch delete update context #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DBClient.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'DBClient'
s.version = '1.4.2'
s.version = '1.4.3'
s.requires_arc = true
s.summary = 'CoreData & Realm wrapper written on Swift'
s.homepage = ''
Expand Down
6 changes: 3 additions & 3 deletions DBClient/CoreData/CoreDataChange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ enum CoreDataChange<T> {

var isDeletion: Bool {
switch self {
case .delete(_): return true
case .delete(_, _): return true
default: return false
}
}

var isUpdate: Bool {
switch self {
case .update(_): return true
case .update(_, _): return true
default: return false
}
}

var isInsertion: Bool {
switch self {
case .insert(_): return true
case .insert(_, _): return true
default: return false
}
}
Expand Down
15 changes: 4 additions & 11 deletions DBClient/CoreData/CoreDataDBClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public class CoreDataDBClient {

private lazy var managedObjectModel: NSManagedObjectModel = {
guard let modelURL = self.bundle.url(forResource: self.modelName, withExtension: "momd"),
let objectModel = NSManagedObjectModel(contentsOf: modelURL) else {
fatalError("Can't find managedObjectModel named \(self.modelName) in \(self.bundle)")
let objectModel = NSManagedObjectModel(contentsOf: modelURL) else {
fatalError("Can't find managedObjectModel named \(self.modelName) in \(self.bundle)")
}

return objectModel
Expand Down Expand Up @@ -506,16 +506,9 @@ extension CoreDataDBClient: DBClient {

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: type.entityName)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
deleteRequest.resultType = .resultTypeObjectIDs
performWriteTask { [weak mainContext] context, savingClosure in
performWriteTask { context, savingClosure in
do {
let result = try context.execute(deleteRequest) as? NSBatchDeleteResult
if let objectIDs = result?.result as? [NSManagedObjectID] {
for objectID in objectIDs {
guard let object = mainContext?.object(with: objectID) else { continue }
mainContext?.delete(object)
}
}
try context.execute(deleteRequest)
try savingClosure()
completion(.success(()))
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ final class CoreDataExecuteTests: DBClientCoreDataTest {

self.dbClient.insert(randomUsers) { result in
if result.value != nil {
let request = FetchRequest<User>(sortDescriptor: sortDescriptor)
let request = FetchRequest<User>(sortDescriptors: [sortDescriptor])

self.dbClient.execute(request) { result in
expectedUsers = result.value ?? []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ final class RealmExecuteTests: DBClientRealmTest {

self.dbClient.insert(randomUsers) { result in
if result.value != nil {
let request = FetchRequest<User>(sortDescriptor: sortDescriptor)
let request = FetchRequest<User>(sortDescriptors: [sortDescriptor])

self.dbClient.execute(request) { result in
expectedUsers = result.value ?? []
Expand Down
164 changes: 88 additions & 76 deletions Example/Example.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions Example/Example.xcodeproj/xcshareddata/xcschemes/Example.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1200"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C53372551E26155D004ECBCF"
BuildableName = "Example.app"
BlueprintName = "Example"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "B8275AFC1E4B6D2500232EE4"
BuildableName = "DBClientTests.xctest"
BlueprintName = "DBClientTests"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C53372551E26155D004ECBCF"
BuildableName = "Example.app"
BlueprintName = "Example"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C53372551E26155D004ECBCF"
BuildableName = "Example.app"
BlueprintName = "Example"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
12 changes: 6 additions & 6 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
PODS:
- DBClient/Core (1.4.2):
- DBClient/Core (1.4.3):
- YALResult (= 1.4)
- DBClient/CoreData (1.4.2):
- DBClient/CoreData (1.4.3):
- DBClient/Core
- YALResult (= 1.4)
- DBClient/Realm (1.4.2):
- DBClient/Realm (1.4.3):
- DBClient/Core
- RealmSwift (~> 3.15.0)
- YALResult (= 1.4)
Expand All @@ -20,7 +20,7 @@ DEPENDENCIES:
- DBClient/Realm (from `../`)

SPEC REPOS:
https://github.com/cocoapods/specs.git:
https://github.com/CocoaPods/Specs.git:
- Realm
- RealmSwift
- YALResult
Expand All @@ -30,11 +30,11 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
DBClient: 6833b6f3abb9bbd90dc3289621179f3fad102c0c
DBClient: cad8d754cd7ae07256c75f23108ab9b2167398a0
Realm: 9b834e1be6062f544805252c812348872dc5d4ed
RealmSwift: 8a41886f8ab6efef9eb8df97de2f2bb911561a79
YALResult: 26915691cdd19269936336d6f28e1a015c64175e

PODFILE CHECKSUM: 01e2cf56f70348c45b9e7248a505591dd86d210b

COCOAPODS: 1.6.1
COCOAPODS: 1.9.3
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public func upsertManagedObject(in context: NSManagedObjectContext, existedInsta

| Version | Swift | Dependencies | iOS |
|----------|-------|----------------------------------------|------|
| `1.4.2` | 5 | RealmSwift 3.15.0, YALResult 1.4 | 10 |
| `1.4.3` | 5.3 | RealmSwift 3.15.0, YALResult 1.4 | 10 |
| `1.3` | 4.2 | RealmSwift 3.11.1, YALResult 1.1 | 10 |
| `1.0` | 4.2 | RealmSwift 2.10.1, YALResult 1.0 | 10 |
| `0.7` | 4.0 | RealmSwift 2.10.1, BoltsSwift 1.4 | 9 |
Expand Down