Skip to content

Commit 0287a23

Browse files
committed
operation queue vs dispatch serial queue
1 parent 24c330b commit 0287a23

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import UIKit
2+
3+
/**
4+
Hey there, I hope the video was helpful. Please feel free to ask questions regarding the topic I will be happy to answer them for you. Do share the topic with your iOS group on facebook or whatsapp and if you are new to the channel please do support the channel by subscribing to it.
5+
6+
Thank you
7+
Ravi (Codecat15) :)
8+
9+
*/
10+
11+
struct Employee
12+
{
13+
func syncOfflineEmployeeRecords() {
14+
15+
debugPrint("Syncing offline records for employee started")
16+
Thread.sleep(forTimeInterval: 2)
17+
debugPrint("Syncing completed for employee")
18+
}
19+
}
20+
21+
struct Department
22+
{
23+
func syncOfflineDepartmentRecords() {
24+
25+
debugPrint("Syncing offline records for department started")
26+
Thread.sleep(forTimeInterval: 2)
27+
debugPrint("Syncing completed for department")
28+
}
29+
}
30+
31+
struct SyncManager
32+
{
33+
func syncOfflineRecords() {
34+
35+
/** IMPORTANT!!! DO NOT DISTURB THE ORDER OF DEPARTMENT AND EMPLOYEE **
36+
37+
As per user story 1234 we need to send department record first and then employee record so please do not break the sequence here
38+
39+
We are using serial queue where sequence matters and if any change is expected in the upcoming release then please make your changes after the employee async
40+
41+
If any other entity needs to be synced before department then please write that code above the department serial queue
42+
43+
*/
44+
45+
// let serialQueue = DispatchQueue.init(label: "codecat15Example")
46+
//
47+
// serialQueue.async {
48+
// let department = Department()
49+
// department.syncOfflineDepartmentRecords()
50+
// }
51+
//
52+
// serialQueue.async {
53+
// let employee = Employee()
54+
// employee.syncOfflineEmployeeRecords()
55+
// }
56+
//
57+
// serialQueue.async {
58+
// debugPrint("syncing project record")
59+
// }
60+
61+
let employeeSyncOperation = BlockOperation()
62+
employeeSyncOperation.addExecutionBlock {
63+
let employee = Employee()
64+
employee.syncOfflineEmployeeRecords()
65+
}
66+
67+
let departmentSyncOperation = BlockOperation()
68+
departmentSyncOperation.addExecutionBlock {
69+
let department = Department()
70+
department.syncOfflineDepartmentRecords()
71+
}
72+
73+
employeeSyncOperation.addDependency(departmentSyncOperation)
74+
75+
let operationQueue = OperationQueue()
76+
operationQueue.addOperation(employeeSyncOperation)
77+
operationQueue.addOperation(departmentSyncOperation)
78+
}
79+
}
80+
81+
let obj = SyncManager()
82+
obj.syncOfflineRecords()
83+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

OperationQueue_vs_Dispatch_SerialQueue/OperationQueue_Vs_DispatchSerial_Queue.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)