Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Add function to batch delete item
Browse files Browse the repository at this point in the history
  • Loading branch information
Luigi Parpinel committed Feb 6, 2019
1 parent ac7306d commit 10b635a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
22 changes: 22 additions & 0 deletions JustPersist/Classes/Core/Implementations/CoreDataAccessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ extension CoreDataAccessor: DataStoreReadWriteAccessor {
return true
}

func batchDeleteAllItems(ofMutableTypes itemTypes: [MutableDataStoreItem.Type]) {

var changedManagedObjectsIds: [NSManagedObjectID] = []

for itemType in itemTypes {
let fetchRequest = DataStoreRequest(itemType: itemType).fetchRequest()
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
deleteRequest.resultType = .resultTypeObjectIDs

do {
let result = try context.execute(deleteRequest) as? NSBatchDeleteResult
let deletedObjectsIds = result?.result as? [NSManagedObjectID] ?? []
changedManagedObjectsIds.append(contentsOf: deletedObjectsIds)
} catch {
assert(false, "This should be a programmatic error")
}
}

let changes = [NSDeletedObjectsKey: changedManagedObjectsIds]
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [context])
}

func mutableVersion(ofItem item: DataStoreItem) -> MutableDataStoreItem? {

guard let object = item as? NSManagedObject, let objectID = item.uniqueToken as? NSManagedObjectID else {
Expand Down
7 changes: 7 additions & 0 deletions JustPersist/Classes/Core/Interface/DataStoreAccessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ public protocol DataStoreReadWriteAccessor: DataStoreReadAccessor {
@discardableResult
func deleteAllItems(ofMutableType itemType: MutableDataStoreItem.Type) -> Bool

/**
Delete all items of the given mutable type in the data store

- parameter [itemType]: The mutable types for which all items should be deleted
*/
func batchDeleteAllItems(ofMutableTypes itemTypes: [MutableDataStoreItem.Type])

// MARK: Mutable Version of Immutable Item

/**
Expand Down

0 comments on commit 10b635a

Please sign in to comment.