last day (14 days later) » 

5:58 PM
1
Q: Running a task on private queue and returning callback

BoonMy method runs its code in a private queue and upon completing, will call the callback passed in. Is there a need to check if the callback passed in is intended to be run from the main queue? e.g. - (void)doSomethingWithCalback:(void(^)())callback { dispatch_async(self.privateQueue, ^{ ...

 
Rob
I do not like the proposed code, because now your behavior changes depending upon which queue you called it from. I'd suggest dispatching it to the main queue or not. Or, if you need more flexibility, you can, as RyanR suggested, provide a parameter to the method to specify which queue to use.
 
I figure if you are calling it from main thread code, chances are you won't expect things to come back running in a background thread instead. Providing an onQueue parameter seems like exposing too much of an implementation detail that has nothing to do with the task at hand.
 
Rob
I understand your intent, but if you're running it from some other thread, the completion handler will now be called on some unknown thread, neither main thread nor the thread it was called from. Personally, I'd be inclined to dispatch it back to the main queue or don't. Your "sometimes main thread" approach is only more confusing, IMHO, and certainly non-standard. And if you want the flexibility to specify, then add a parameter.
 
Not sure why the thread thing is an issue - we only care about queue (specifically, whether the code should be on main queue or not), no? If you call the method from a view controller, you would expect callback to be run on main queue because you might do UI update there. Otherwise you don't care.
 
Rob
Don't get hung on on the thread/queue distinction, because my point is the same if we talk about queues. Your approach means that you sometimes know what queue will be used (i.e. if you called it from main queue) but sometimes won't (i.e. if you called from a particular background queue, this completion handler will not be called on that queue, nor on the main queue).
 
5:58 PM
Agree - that's really my point though, the user shouldn't have to care what queue will be used, hence no argument will be provided. We are simply taking the safe route of making sure code calling from main thread will be treated with caution and have the callback run there as well - because as a user, you certainly won't think that your callback will be run in a background queue instead.
 
Rob
In your example, you say you don't care what queue is used if not main queue, but when you use special queues (e.g. a synchronization serial or reader-writer queue) suddenly which background queue you use really matters.
 
hi
 
Rob
Hey.
 
thanks for answering my question
I was designing things on the intent to minimize surprises
 
Rob
Then just always dispatch to the main queue and be done with it.
 
5:59 PM
if we call something from view controller, chances are we don't think our callback will be run from a private queue instead
but if i am calling the code from a background thread
why would i want to clog up main queue?
 
Rob
I wouldn't assume that you should always expect the callback on the main thread just because you called it from the view controller. Many Cocoa callbacks are called on random background queues (e.g. NSURLSession).
 
what am i assuming?
I didn't say I assume it would be called from main queue
why would you expect your callback to be running from
if not main?
 
Rob
?
 
if I am making a call that is not obvious that it involves networking, why would i make the assumption my callback will be in background?
i think what i am doing there seems safe
if you call from main thread, I will keep it safe for you
if not, i won't
that's a step better than just dumping everything on main queue
 
Rob
Hey, you're determined to use a non-standard convention, so go ahead and do that. I just don't think it's a good solution. My point is that Cocoa methods with completion handlers either (a) make no assurances regarding the callback queue; (b) guarantee that the callback will be on the main queue; or (c) give you the option to specify what queue to use.
You're shunning all of those standard approaches and changing it so that the callback's queue changes determining upon what queue you called it from. It's non-standard, for little benefit.
 
6:04 PM
i am not sure what you mean by standard
where is the standard?
i would love to learn that
 
Rob
lol.
 
and stick to it if there is such standard
 
Rob
Look at the Cocoa API. They all use one of those three approaches. None of them use the approach you suggest.
 
i still don't get it, just cuz they are not using means it's bad?
UIKit used to advocate people use weak IBOutlet
now they are asking people to use strong
btw, do you have some examples of (a), (b) and (c)?
 
Rob
You want to write code that adopts common conventions. I'd only introduce a new convention if there was a compelling need. This sort of deviation of established conventions is the sort of thing that will be really confusing when you come back to this code after a year or two. If you stick with established conventions, it will makes maintenance of your code easier.
 
6:09 PM
I checked NSURLSession, it seems to make sure things run on the delegate queue
 
Rob
Sometimes there's a compelling reason to adopt a new convention, then, fine, do that. But make sure that's explicit (e.g. add a comment that shows up in the quick reference that brings the non-standard behavior to the attention of the programmer).
 
I agree with you - just didn't know there is a such a convention
not arguing, trying to learn here
How does NSURLSession make it so the callback runs on the delegate queue?
it seems like NSURLSession would run things on main queue if you call it from main queue
downloadTaskWithURL as an example
 
Rob
(a) NSURLSession; (b) CLGeocoder; (c) NSURLSession with sessionWithConfiguration:delegate:queue:.
No, with NSURLSession completion handlers, you have no assurances as to which queue they run on.
 
the doc says
it will run it in the delegate queue
"The completion handler to call when the load request is complete. This handler is executed on the delegate queue."
You do have assurance
 
Rob
Yeah, which is a queue you create when you instantiate the NSURLSession. And if you don't specify that queue, you have no assurances as to what queue will be used (i.e. it won't use the main queue).
 
6:15 PM
How do I specify the queue?
 
Rob
sessionWithConfiguration:delegate:queue:
 
ok I saw it
the property
 
Rob
The property is read only. You specify it when you call sessionWithConfiguration:delegate:queue:.
 
on GLGeocoder
which call ensures completion handler is run on main queue?
Looked up doc, didn't find the description on that
 
Rob
CLGeocoder has reverseGeocodeLocation:completionHandler:, for example.
"Your completion handler block will be executed on the main thread."
 
6:18 PM
ok saw it
cool
I will just do what you suggest
always do it on main thread then
thanks for the points brought up, can you include the three cases in your answer?
 
Rob
Yeah, for simple applications, that's often easiest.
 
i think your answer is more complete
i will select that
 
Rob
As your app gets more complicated, you'll adopt a pattern like RyanR's (though I disagree with him on a detail: if you use that pattern, you'd generally default to unknown if the user doesn't specify a queue, but give them the option of specifying main queue if they want)
 
true
 

last day (14 days later) »