Skip to content

Commit 32972b0

Browse files
author
Dilip Parmar
committed
changes for deleteAsyncBy
1 parent 80d8ed4 commit 32972b0

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

CoreDataWrapper_iOSTests/AsyncOperationsTests.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ class AsyncOperationsTests: XCTestCase {
111111

112112
let expectation = XCTestExpectation.init(description: "\(#file)\(#line)")
113113

114-
coreDataWrapper.deleteAsyncBy(objectId: car!.objectID, shouldSave: true, completion: {
114+
coreDataWrapper.deleteAsyncBy(objectId: car!.objectID, shouldSave: true, completion: { (isDeleted) in
115+
116+
XCTAssert(isDeleted)
117+
115118
let fetched = coreDataWrapper.fetchBy(objectId: car!.objectID) as? Car
116119
XCTAssertNil(fetched)
117120
expectation.fulfill()
@@ -572,7 +575,10 @@ class AsyncOperationsTests: XCTestCase {
572575
let expectation = XCTestExpectation.init(description: "\(#file)\(#line)")
573576
let context = coreDataWrapper.newBgContext()
574577

575-
coreDataWrapper.deleteAsyncBy(objectId: car!.objectID, context: context, shouldSave: true, completion: {
578+
coreDataWrapper.deleteAsyncBy(objectId: car!.objectID, context: context, shouldSave: true, completion: { (isDeleted) in
579+
580+
XCTAssert(isDeleted)
581+
576582
let fetched = coreDataWrapper.fetchBy(objectId: car!.objectID) as? Car
577583
XCTAssert(fetched!.isDeleted)
578584

@@ -595,7 +601,10 @@ class AsyncOperationsTests: XCTestCase {
595601

596602
let expectation = XCTestExpectation.init(description: "\(#file)\(#line)")
597603
let context = coreDataWrapper.newBgContext()
598-
coreDataWrapper.deleteAsyncBy(objectId: car!.objectID, context: context, shouldSave: true, completion: {
604+
coreDataWrapper.deleteAsyncBy(objectId: car!.objectID, context: context, shouldSave: true, completion: { (isDeleted) in
605+
606+
XCTAssert(isDeleted)
607+
599608
let fetched = coreDataWrapper.fetchBy(objectId: car!.objectID) as? Car
600609
XCTAssert(fetched!.isDeleted)
601610
XCTAssert(Thread.isMainThread)

CoreDataWrapper_iOSTests/SyncOperationsTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ class SyncOperationsTests: XCTestCase {
9292
let car = coreDataWrapper.addOf(type: Car.self, properties: ["model": "Audi", "regNo": 30], shouldSave: false)
9393
XCTAssertNotNil(car)
9494

95-
coreDataWrapper.deleteBy(objectId: car!.objectID, shouldSave: true)
95+
let isDeleted = coreDataWrapper.deleteBy(objectId: car!.objectID, shouldSave: true)
96+
XCTAssert(isDeleted)
9697

9798
let fetched = coreDataWrapper.fetchBy(objectId: car!.objectID) as? Car
9899
XCTAssertNil(fetched)

Sources/CoreDataWrapper/CoreDataWrapper+AsyncOperations.swift

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ extension CoreDataWrapper {
308308
(objectId: NSManagedObjectID,
309309
context: NSManagedObjectContext? = nil,
310310
shouldSave: Bool,
311-
completion: @escaping () -> Void,
311+
completion: @escaping (Bool) -> Void,
312312
completionOnMainThread: Bool) {
313313
var innerContext: NSManagedObjectContext
314314
if let context = context {
@@ -317,53 +317,55 @@ extension CoreDataWrapper {
317317
innerContext = self.mainContext
318318
}
319319
innerContext.perform {
320+
var isDeleted = false
320321
if let existingObject = try? innerContext.existingObject(with: objectId),
321322
!existingObject.isDeleted {
322323
innerContext.delete(existingObject)
324+
isDeleted = true
323325
}
324-
let saveMain = { (completion: @escaping () -> Void) in
325-
self.saveMainContext(isSync: false, completion: { (Bool) in
326-
completion()
326+
let saveMain = { (completion: @escaping (Bool) -> Void) in
327+
self.saveMainContext(isSync: false, completion: { (isSuccess) in
328+
completion(isSuccess && isDeleted)
327329
})
328330
}
329-
let saveBG = { (completion: @escaping () -> Void) in
330-
self.saveBGContext(context: innerContext, isSync: true, completion: { (Bool) in
331-
completion()
331+
let saveBG = { (completion: @escaping (Bool) -> Void) in
332+
self.saveBGContext(context: innerContext, isSync: true, completion: { (isSuccess) in
333+
completion(isSuccess && isDeleted)
332334
})
333335
}
334-
let mainCaller = {
336+
let mainCaller = { (isSuccess: Bool) in
335337
self.mainContext.perform {
336-
completion()
338+
completion(isSuccess)
337339
}
338340
}
339-
let bgCaller = {
340-
completion()
341+
let bgCaller = { (isSuccess: Bool) in
342+
completion(isSuccess)
341343
}
342344
let tuple = (completionOnMainThread, (context != nil), shouldSave)
343345
switch tuple {
344346
case (false, false, false): //It's main context and no main thread callback
345-
bgCaller()
347+
bgCaller(isDeleted)
346348
case (false, false, true): //It's main context and no main thread callback
347-
saveMain({
348-
bgCaller()
349+
saveMain({ (isSuccess: Bool) in
350+
bgCaller(isSuccess)
349351
})
350352
case (false, true, false): //It's bg context and no main thread callback
351-
bgCaller()
353+
bgCaller(isDeleted)
352354
case (false, true, true): //It's bg context and no main thread callback
353-
saveBG({
354-
bgCaller()
355+
saveBG({ (isSuccess: Bool) in
356+
bgCaller(isSuccess)
355357
})
356358
case (true, false, false): //It's main context and main thread callback
357-
bgCaller()
359+
bgCaller(isDeleted)
358360
case (true, false, true): //It's main context and main thread callback
359-
saveMain({
360-
bgCaller()
361+
saveMain({ (isSuccess: Bool) in
362+
bgCaller(isSuccess)
361363
})
362364
case (true, true, false): //It's bg context and main thread callback
363-
mainCaller()
365+
mainCaller(isDeleted)
364366
case (true, true, true): //It's bg context and main thread callback
365-
saveBG({
366-
mainCaller()
367+
saveBG({ (isSuccess: Bool) in
368+
mainCaller(isSuccess)
367369
})
368370
}
369371
}
@@ -513,12 +515,12 @@ extension CoreDataWrapper {
513515
}
514516
let saveMain = { (completion: @escaping (Bool) -> Void) in
515517
self.saveMainContext(isSync: false, completion: { (isSuccess) in
516-
completion(isSuccess)
518+
completion(isSuccess && isUpdated)
517519
})
518520
}
519521
let saveBG = { (completion: @escaping (Bool) -> Void) in
520522
self.saveBGContext(context: innerContext, isSync: true, completion: { (isSuccess) in
521-
completion(isSuccess)
523+
completion(isSuccess && isUpdated)
522524
})
523525
}
524526
let mainCaller = { (updateResult: Bool) in

0 commit comments

Comments
 (0)