热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何将基本数据保存到数组中-HowtosaveFirebaseDataintoanArray

IhaveaFirebasedatabasethatismodeledassuch:我有一个这样建模的Firebase数据库::users:some-random

I have a Firebase database that is modeled as such:

我有一个这样建模的Firebase数据库:

: users
    : some-random-id-1
        - username:"user1"
        - email:"email1@gmail.com"
    : some-random-id-2
        - username:"user2"
        - email:"email2@gmail.com"

I am trying to iterate through all the users in the dictionary of data and append the username into a list in the file to be used for other purposes. I created a string array (userslist) variable and in viewdidload(), I wrote the following code below:

我尝试遍历数据字典中的所有用户,并将用户名附加到文件中的列表中,以便用于其他目的。我创建了一个字符串数组(userslist)变量,在viewdidload()中,我编写了以下代码:

    ref = Database.database().reference()
    ref?.observe(.value, with: { (snapshot) in
        let dataDict = snapshot.value as! NSDictionary
        let x = dataDict["users"] as! NSDictionary
        print(x)
        print("--------")
        for user in x{
            let y = user.value as? [String: String]
            let z = y!["username"]
            print(z)
            self.userslist.append(z!)
            print(self.userslist)
            print("NEXT")
        }
    })
    print(self.userslist)

Inside the brackets of the snapshot, when I print self.userslist, I can see that each element is getting added, but when I print it a final time outside of the brackets, it shows it as an empty array. I think that the elements are only appended in the scope of those brackets so I cant access the filled array anywhere else. How do I get around this so I can use the data I appended?

在快照的括号内,打印self。userslist,我可以看到每个元素都被添加了,但是当我最后一次在括号外打印时,它显示为一个空数组。我认为这些元素只添加在括号的范围内,所以我无法访问填充数组。如何绕过这个,以便使用我添加的数据?

1 个解决方案

#1


1  

you are using print(self.userslist) outside the observer and Firebase run in Async Mode

您正在使用observer之外的print(self.userslist)和在异步模式下运行的Firebase

So, if you make use of breakpoints you will notice that

如果你利用断点,你会注意到

print(self.userslist) is Called before the control reach onside the Database handler ,

在控件到达数据库处理程序之前调用print(self.userslist),

data is getting fetched you need to load your views inside that handler using Dispatch main queue

数据正在获取,您需要使用分派主队列在处理程序中加载视图

    ref?.observe(.value, with: { (snapshot) in
            let dataDict = snapshot.value as! NSDictionary
            let x = dataDict["users"] as! NSDictionary
            print(x)
            print("--------")
            for user in x{
                let y = user.value as? [String: String]
                let z = y!["username"]
                print(z)
                self.userslist.append(z!)
                print(self.userslist)
                print("NEXT")
            }
            /// Here is your data
            print(self.userslist)
        })
 /// Called before Handler execution
 print(self.userslist)

推荐阅读
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • Givenasinglylinkedlist,returnarandomnode'svaluefromthelinkedlist.Eachnodemusthavethe s ... [详细]
  • 本文介绍了GregorianCalendar类的基本信息,包括它是Calendar的子类,提供了世界上大多数国家使用的标准日历系统。默认情况下,它对应格里高利日历创立时的日期,但可以通过调用setGregorianChange()方法来更改起始日期。同时,文中还提到了GregorianCalendar类为每个日历字段使用的默认值。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文介绍了MVP架构模式及其在国庆技术博客中的应用。MVP架构模式是一种演变自MVC架构的新模式,其中View和Model之间的通信通过Presenter进行。相比MVC架构,MVP架构将交互逻辑放在Presenter内部,而View直接从Model中读取数据而不是通过Controller。本文还探讨了MVP架构在国庆技术博客中的具体应用。 ... [详细]
  • 上图是InnoDB存储引擎的结构。1、缓冲池InnoDB存储引擎是基于磁盘存储的,并将其中的记录按照页的方式进行管理。因此可以看作是基于磁盘的数据库系统。在数据库系统中,由于CPU速度 ... [详细]
  • 开源Keras Faster RCNN模型介绍及代码结构解析
    本文介绍了开源Keras Faster RCNN模型的环境需求和代码结构,包括FasterRCNN源码解析、RPN与classifier定义、data_generators.py文件的功能以及损失计算。同时提供了该模型的开源地址和安装所需的库。 ... [详细]
  • Netty源代码分析服务器端启动ServerBootstrap初始化
    本文主要分析了Netty源代码中服务器端启动的过程,包括ServerBootstrap的初始化和相关参数的设置。通过分析NioEventLoopGroup、NioServerSocketChannel、ChannelOption.SO_BACKLOG等关键组件和选项的作用,深入理解Netty服务器端的启动过程。同时,还介绍了LoggingHandler的作用和使用方法,帮助读者更好地理解Netty源代码。 ... [详细]
  • OpenMap教程4 – 图层概述
    本文介绍了OpenMap教程4中关于地图图层的内容,包括将ShapeLayer添加到MapBean中的方法,OpenMap支持的图层类型以及使用BufferedLayer创建图像的MapBean。此外,还介绍了Layer背景标志的作用和OMGraphicHandlerLayer的基础层类。 ... [详细]
  • 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。 2.对于随机访问get和set,ArrayList优于LinkedList,因为Ar ... [详细]
  • 一、死锁现象与递归锁进程也是有死锁的所谓死锁:是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作 ... [详细]
  • ext将html代码转为字符串,在iOS中将HTML转换为NSAttributedString
    在iOS7中,UIKit添加了一个initWithData:options:documentAttributes:error ... [详细]
  • 本文详细介绍了Android中的坐标系以及与View相关的方法。首先介绍了Android坐标系和视图坐标系的概念,并通过图示进行了解释。接着提到了View的大小可以超过手机屏幕,并且只有在手机屏幕内才能看到。最后,作者表示将在后续文章中继续探讨与View相关的内容。 ... [详细]
author-avatar
拍友2702932701
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有