I've been using firebase database and now firestore for a while. I'm prob an intermediate level programmer and still at uni. I'm still a bit unsure re: the best way to handle calls that are occurring async to firebase database and updating the UI. I've previously used delegates to do this but I was wondering whether GCD and Dispatch.main.async - which I don't fully understand - are an alternative way.
-
It appears that it is/they are, based on my cursory googling.Robert Harvey– Robert Harvey2017-11-25 16:09:00 +00:00Commented Nov 25, 2017 at 16:09
-
Ok. Thx. So my next question is can i get some pointers on how I would implement GCD and Dispatch.main.async with a call to the firestore db?Mike– Mike2017-11-25 21:02:15 +00:00Commented Nov 25, 2017 at 21:02
-
Comments aren't the place to ask new questions but for clarifications & to make suggestions. Have you tried searching the SE family of sites?outis– outis2017-11-26 20:10:50 +00:00Commented Nov 26, 2017 at 20:10
1 Answer
GCD and Delegation solve different problems. Delegation is Apple's name for the Strategy Pattern while GCD is an API library for managing threads (updated below.) Because of this, your question doesn't make a whole lot of sense. There are plenty of situations where you would use both GCD and delegation.
My guess is that you are actually asking about Delegation vs passing closures as callbacks. I think the Swift community is still working out best-practices in this regard so it's hard to give any definite rules.
Generally though, I would say that:
- If the Delegate would require an associated type (or using
Anyand casting), then a closure would probably be better. If the closure requires that an enum be passed with more than a couple of cases, then a delegate would be better. - If the Delegate would end up with only one method, then prefer a closure. If the closure must be passed as an @escaping parameter then prefer a Delegate.
The above are not mutually exclusive. Personal preference figures heavily here. In all cases, you want to cater to the needs of the code that provides the Delegate/closure. Whatever makes life easier for that code should be your guide.
EDIT
Josh Caswell has called me out on my lack of precision regarding GCD. Per Apple's documentation, GCD is a framework for "[executing] code concurrently on multicore hardware by submitting work to dispatch queues managed by the system."
However, he is wrong about the Delegation pattern being what is referred to when talking about a "Delegate" in Apple's descriptions. Equating Apple's delegates with the Strategy pattern is more correct.
-
"Delegation is Apple's name for the Strategy Pattern" Delegation is Apple's name for the Delegation Pattern. "GCD is an API library for managing threads" GCD provides an API whereby you don't have to -- in fact, can't -- work with threads. It is a complete alternative to managing threads.jscs– jscs2017-11-27 13:51:17 +00:00Commented Nov 27, 2017 at 13:51
-
This might be a separate question but re: GCD and Dispatch.main.async if the idea is that this allows updating of the UI on the main thread isn't that a synchronous task? Within Dispatch.main.async{do something to the UI} it's jumping onto the main thread to update the UI which then makes the main thread wait?Mike– Mike2017-11-28 02:04:17 +00:00Commented Nov 28, 2017 at 2:04
-
@Mike No, it's an async task (hence the name
async.) DispatchQueue.main.async will put the block of work on the main queue and then return without actually executing the block of work. That way the thread that is calling async isn't being blocked. The work will be executed the next time through the UI Loop. When the work is executing, the main thread isn't waiting, it's executing the work.Daniel T.– Daniel T.2017-11-28 02:14:17 +00:00Commented Nov 28, 2017 at 2:14