small medium large xlarge

07 Apr 2016, 17:47
Victor Jalencas (2 posts)

When discussing about the Export Operations (and operations in general that we may need to perform with our managed objects) it is explained that, as much as possible, work should be shipped to a background queue to not affect the UI thread.

Yet on the PPRDetailViewController.swift file, at the end of the mailRecipe() method, the operation is added to the main queue. This confuses me a bit, because the operation’s main() seems to expect to be run under a non-main queue (since it avoids using anything other than the objectId, and it uses its own MOC.) On the other hand, the code calls performBlockAndWait(), so that block is indeed run on a background thread.

Or that’s what I thought, but when I ran the code to confirm my guess, I was surprised to find that the block was running on the main thread o_O I thought I understood operation queues, but I am now more confused than ever

So, in order to ship work to a background thread, should I:

  1. add the operations to a background queue?
  2. or, it does not matter since the performBlock… methods will take care of running that work on the thread of the child MOC anyway?



07 Apr 2016, 18:09
Marcus S. Zarra (284 posts)

That is actually an error in the code that I will correct before publication.

The call to NSOperationQueue.mainQueue() will actually provide a unique NSOperationQueue that is different than the one you would get from an init. The difference, as you discovered is that it runs all of its operations on the mainQueue.

The correction is to properly initialize a NSOperationQueue instead of using that one.

08 Apr 2016, 16:15
Victor Jalencas (2 posts)

thanks for the response Marcus. I assume that queue would be owned by the dataController then. What are the best options to create it (regarding priority and so on)?

Also, I’m considering separating on my app the DataController from the NetworkController (as mentioned in your MVC-N talk, though if I understood you correctly you consider it a single one), so that one takes care of the persistence and the other of the network requests. What pros/cons do you see in that approach?

08 Apr 2016, 16:19
Marcus S. Zarra (284 posts)

Yes, the queue would be owned by the data controller. I generally start with just a basic init of the queue and tweak things later when I am doing performance optimizations or in response to changes to the network conditions.

I have never separated the data controller and the network controller. Every time I have thought about doing it I discover how much inter-communication will occur between the two controllers and then I end up putting them together :)

If you are going to do that, map out how much the network controller needs the data controller and how much the data controller is going to be accessing the network controller

You must be logged in to comment