i am adding the operations to the NSOperationQueue in asynchronously. I have a requirement like I need to change the priority of the operations like I have A,B,C operations are added to the queue and I need to execute the C operation at first. If I get some error executing I have to that operation to the Queue with high priority. I googled about it, we can't do like this. By using GCD we can do like this using notify and wait. I have a query, if I added the A operation in asynchronously and B and C added in group notify. In that A operation will execute firstly after that B and C will execute. If I get the error while executing operation A then I added this to the group until and unless operation get success. Here in this process if after adding the Operation A which one will execute firstly Operation A or Operation B/C. In my requirement operation A has to execute firstly. I am new to blocks, Please suggest me if we can implement through Queues or GCD. Please provide some related code. I need to execute the one task at time .Also I am using maxConcurrentOperationCount=1.
Asked
Active
Viewed 742 times
1 Answers
0
Neither NSOperationQueue or GCD queues are meant to be priority queues. They are mostly FIFO queues with some additional smartness. Also they shine best if you need multiple concurrent operations.
In your scenario (maxConcurrentOperationCount=1 with custom scheduling) a plain old NSThread is just fine.
First you need to implement a priority queue of things or reuse a library that does that (one example - atljeremy PriorityQueue, but you'll probably have to adapt it to support modification of priorities after the item was added.
Then just run a loop (inside an NSThread) that takes a task from the queue and runs it:
while (!isStopped) {
NSOperation *task = [queue poll];
[task main];
}
You can reuse NSOperation or make your own task type, that's up to you. You can also use dispatch_block_t
blocks as tasks.

battlmonstr
- 5,841
- 1
- 23
- 33
-
Thanks for the response, I just implemented GCD like below. dispatch_group_t group = dispatch_group_create(); if ([[props valueForKey:@"StartedSessn"] boolValue]){ dispatch_group_enter(group); [self pushEvnts:ev withProperties:props group:group]; /// after getting the success response from server i just leave the group. }else{ dispatch_group_notify(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { [self pushEvnts:ev withProperties:props group:group]; }); } – Naveen Reddy May 08 '18 at 13:07
-
one thing in that code if firstly task added to notify group and then after that the another task is added to Enter group. In that case which one will execute firstly. Please suggest. – Naveen Reddy May 08 '18 at 13:12
-
I don't quite get how are you able to change priorities of previously added task using this code. – battlmonstr May 09 '18 at 09:14
-
Ya actually I am adding the tasks to notify group except the started session task . This method is wait until the leave group called right..I am not sure I am doing right, please suggest me if I am doing wrong.. – Naveen Reddy May 09 '18 at 09:21
-
Tell me what are you trying to achieve? What are your tasks doing? Why do you want to change the prio? – battlmonstr May 09 '18 at 09:23
-
actually I am calling the multiple services asynchronously. Here I am adding the methods to operation queue. Actually I have to call the Session Start method before calling other methods. So I am trying to change the priority – Naveen Reddy May 09 '18 at 09:33
-
Why don't you just start your multiple services operations as soon as "Session Start" returns? – battlmonstr May 09 '18 at 11:46
-
After initialising my sdk in app the session will get started. While Session get started I am getting the information from server and then I am pushing some information to my server. In that process if user make some action, then I have to hold that actions until and unless started session will be executed. – Naveen Reddy May 09 '18 at 12:07
-
To fulfil that I am using the queues. – Naveen Reddy May 09 '18 at 12:08
-
In this case I'd make your SDK init async, and give the object to the user only when it is fully functional, i.e. has the session started. – battlmonstr May 09 '18 at 13:46