iOS Concurrency with GCD and Operations - Part 7: | Ray Wenderlich

@audrey Can you please help with this when you get a chance? Thank you - much appreciated! :]

@shogunkaramazov Iā€™ve been keeping an eye on the discussion, and they seem to have worked everything out better than I could :wink:

Itā€™s still not clear to me - thereā€™s an issue with the tutorial itself? It has issues with thread safety? Iā€™m trying to work on a simple app with networking - and Iā€™ve fallen into a rabbit hole, with PromiseKit, GCD and now Operation Queues. I canā€™t find a simple way to just implement it properly and learn from it. It seems like such a huge topic - is there any good resource online - be it paid to get a grasp on Operation Queues?

For simple iOS app you donā€™t have to use either PromiseKit or OperationQueue.
Please checkout tutorial how to use URLSession APIs (you can also find video series on this topic):
https://www.raywenderlich.com/567-urlsession-tutorial-getting-started

When to use GCD.
For all common cases. Please see example here and fore more checkout these video series video:

When to use OperationQueue?
Simple answer is when you have complex dependency graph (like Download ā†’ Parse ā†’ Decompress ā†’ ā€¦). For more checkout video series example.

PromiseKit is trying to bring up to Swift/ObjcC concept of promises / futures. A future is, in short, an object which hides a calculation / asynchronous work. You donā€™t need it basically unlesss you would want to play with it. I personally donā€™t use it because thereā€™s no needs for that. Also try to avoid using Third Party APIs unless you know what you are doing / get more experienced :slight_smile:

2 Likes

@altairs99 Thank you for your answer. Iā€™ve dived deep into all these topics - and I am trying to watch WWDC videos/tutorias/search forums/SO.

I am current working on an app for reading Hacker News.

  1. I firstly fetch a list of story ids
  2. Then I want to update my Table View with the number of rows and have placeholder graphics until each story is loaded.
  3. Then I need to fetch for each story item, its details. But I want to load them in parallel.
  4. As each story response is received, I want parse and to reload only itā€™s specific Table View row.
    ā€¦
  5. Later I want to try and optimize this even more and make it even more interactive: eg. implement a sort of paging mechanism - load only the first 20 items and as the user starts to scroll to load up more. Load the article which is linked as a preview etc. Thatā€™s why I think I need Operations or Promises or something else.

Iā€™ve tried to implement an Operation Queue following this tutorial example here: https://www.raywenderlich.com/5293-operation-and-operationqueue-tutorial-in-swift

The problem I have is that in the tutorial - the ā€˜download of the imageā€™ is not async as is the case with my request.

  override func main() {
//4
if isCancelled {
  return
}

//5
guard let imageData = try? Data(contentsOf: photoRecord.url) else { return }

whereas i have an URLSession task which when the operation is executed, it considers as finished only by starting the task (sending the request), instead of waiting for it to finish.
Here is my Operation subclass: https://github.com/Mihail87/Timely/blob/master/Timely2/Models/Item.swift

From this tutorial we are discussing here - I understand that I need to manually manage the state of the Operation myself to get this working. Iā€™m still trying to wrap my head around it - as Iā€™ve encountered a lot of new subjects (spoiler alert: Iā€™m a newbie :blush:)

Am I on the right track? I will keep digging anway :slight_smile: but confirmation would help keep my spirits high. Thanks again!

1 Like

I would suggest to start simple and advance from there (E.g. start using URLSession first). Once you have basic version completed you can refactor it with more advanced features you described. If youā€™d like to move forward with OperationQueue and Async operation now, please check WWDC session and their sample code for Async Operations:

2 Likes

Cheers! I already did the first version only with URLSession - but I wanted to learn concurrency :stuck_out_tongue:

2 Likes

I just wanted to add that @audrey 's tutorial from the 2017 Vault convinced me to purchase the 1 year subscription on RayW. I am now going through the tutorials from the beginning, maybe I missed some things. Thanks again guys! @audrey & @altairs99 !

2 Likes

I see KVO being used, I have no idea about it. What topic does it come under?
I read the MVM to MVVM RW blog post, I remember KVO was an option but the article used Boxing instead. Any internal resources to learn more about KVO and get myself familiar with similar general syntaxes?

Just wondering, is isReady set to false when state changes to .Executing?

AsyncOperationā€™s state changing to .Executing causes the Operationā€™s state to change to isExecuting, which sets the other Operation state values to false. An operation can be in only one state at a time.

Yeah right, but at 2:52, the isReady state variable seems to be true even when isExecuting is trueā€¦ which is what my question was about.
image

Also, when the operation state is isFinished, isReady is still true - image
Which makes it look like this was not overlooked, or is it?

hi akashlal! very interesting situation: if you go back to video 6 OperationQueue and check isExecuting && isReady in main(), theyā€™re both true. But do the same in this videoā€™s playground, and only isReady is true. Because in AsyncOperation, start() calls main() before setting its state to .Executing.

Also, because AsyncOperation overrides isReady to be super.isReady && state == .Ready, what I said before is true for AsyncOperation but false for ā€œordinaryā€ operations. Setting state = .Executing makes isReady false because state canā€™t be both .Executing and .Ready at the same time.

I think we donā€™t really have to manage isReady ā€” Appleā€™s documentation says you should only need to manage isReady if ā€œthe readiness of your operations is determined by factors other than dependent operations, ā€¦ such as by some external condition in your programā€. Itā€™s there in AsyncOperation to initialize State, but maybe thereā€™s another way ā€¦ Iā€™ll consult with my colleagues.

Thanks for the question!

1 Like

@akashlal Please check out the updated version of the course when you get a chance:

https://www.raywenderlich.com/9461083-ios-concurrency-with-gcd-and-operations

I hope it helps!

1 Like