@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
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
@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.
- I firstly fetch a list of story ids
- Then I want to update my Table View with the number of rows and have placeholder graphics until each story is loaded.
- Then I need to fetch for each story item, its details. But I want to load them in parallel.
- As each story response is received, I want parse and to reload only itās specific Table View row.
ā¦ - 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 )
Am I on the right track? I will keep digging anway but confirmation would help keep my spirits high. Thanks again!
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:
Cheers! I already did the first version only with URLSession - but I wanted to learn concurrency
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 !
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.
Also, when the operation state is isFinished, isReady is still true -
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!
@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!