Swift Function从异步Firebase调用返回值

编程入门 行业动态 更新时间:2024-10-24 03:24:41
本文介绍了Swift Function从异步Firebase调用返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个函数,该函数接受一个groupchatID(字符串),并为该群聊返回一个收件人列表([String]).我正在为函数的异步部分而苦苦挣扎.当我运行该函数时,它将正确地将我要查找的用户名数组打印到控制台.虽然,当我调用该函数并尝试打印返回的值时,它始终是一个空数组,因为该函数会在Firebase调用完成之前返回该数组.我正在尝试使用回调,但是我不太了解所有语法.请看一下,让我知道需要更改的内容.

I am writing a function that takes a groupchatID (String) and returns a list of Recipients ([String]) for that group chat. I am struggling with the asynchronous part of the function however. When I run the function, it correctly prints to the console the array of usernames I was looking for. Although, when I call the function and try to print the returned value, it is always an empty array because the function returns the array before the firebase call has finished. I am trying to use a callback, but I do not quite understand the syntax of it all. Please take a look and let me know what needs to be changed.

功能:

func GetRecipientsFor(GroupChatID :String , completion: @escaping ([String]) -> ()) { var returnArray: [String] = [""] rootRef.child("chatMembers").child(GroupChatID).observeSingleEvent(of: .value, with: { (snapshot) in for child in snapshot.children.allObjects { var append = child as! FIRDataSnapshot returnArray.append((append.key as String)) print("Return Array Currently Contains: \(returnArray)") //The above printout works properly and when the for loop finishes, the array is exactly as I want it } completion(returnArray) //BUT, this portion returns an empty array }) }

我如何调用该函数:

GetRecipientsFor(GroupChatID: gchatID) { (result) -> () in print(result) }

新功能调用

var recipients : [String] = [""] DispatchQueue.main.async { GetRecipientsFor(GroupChatID: gchatID) { result in print(result) //PRINTS CORRECTLY!!! recipients = result } } print(recipients) //PRINTS A BLANK ARRAY

推荐答案

var recipients : [String] = [""] DispatchQueue.main.async { GetRecipientsFor(GroupChatID: gchatID) { result in print(result) recipients = result } } print(recipients) // Completes before recipients = result

是最后一行发生在异步调用之前.

is that the last line is happening before the async call.

为进一步解释print(recipients)发生在recipients = result之前.使用接收者的所有逻辑都需要在该完成块内发生.您所需要做的就是

To explain futher print(recipients) happens before recipients = result. All logic using recipients needs to happen within that completion block. All you need to do is

func getRecipients(completion: @escaping ([String]) -> ()) { var recipients : [String] = [""] DispatchQueue.main.async { GetRecipientsFor(GroupChatID: gchatID) { result in print(result) completion(result) } } }

如果要包含其他逻辑,可以在补全内调用一个函数,即handleResults(result).我认为阅读有关关闭/completion阻止/和异步调用.

if you want to have further logic included you can call a function within the completion i.e. handleResults(result). I think it would be very beneficial to read more about closures/completion blocks/and async calls.

更多推荐

Swift Function从异步Firebase调用返回值

本文发布于:2023-11-26 18:31:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634698.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:返回值   Swift   Function   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!