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

使用Swift4语言同步Firebase数据库-SynchronizedatabaseofFirebasewithSwift4language

Iamnewatswift4programming.Itrytoshowsomedata(actuallytheyarechildnames)fromdatabas

I am new at swift 4 programming. I try to show some data(actually they are child names) from database of firebase. When view did load, I can take them and show on tableView but it is not working real time. When I delete some data from firebase, I need to refresh(go back and load again) the viewController. If I refresh the tableView with timer, all data append from its end. If I, firstly, delete after append new data to tableView. It is jittering. Is there anyway to synchronize this tableView with firebase, without these problems?

我是swift 4编程的新手。我尝试从firebase数据库中显示一些数据(实际上它们是子名称)。当视图加载时,我可以将它们显示在tableView上,但它不能实时工作。当我从firebase中删除一些数据时,我需要刷新(返回并重新加载)viewController。如果我用计时器刷新tableView,则所有数据都从其末尾追加。如果我,首先在将新数据附加到tableView后删除。它在抖动。反正有没有同步这个tableView与firebase,没有这些问题?

override func viewDidLoad() {
        super.viewDidLoad()

        ref = Database.database().reference()
        ref.keepSynced(true)

        let userRef = self.ref.child("Users").child(Username).child("Schedule")
        userRef.observeSingleEvent(of: .value, with: { snapshot in
            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let key = snap.key
                self.myList.append(key)
                self.CourseList.append(key)
                self.LessonsTableView.reloadData()
            }
            // Lessons are taking and after, they are locating under phone's memory!
            UserDefaults.standard.set(self.CourseList, forKey: "LessonsArray")
            UserDefaults.standard.synchronize()
        })
}

//Setting up our table
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myList.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = LessonsTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LessonsTableViewCell
        cell.myLabell.text = myList[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let Dummy:String = myList[indexPath.row] as Any as! String
        let Selected:String = (Dummy as NSString) as Any as! String
        UserDefaults.standard.set(Selected, forKey: "Selected")
        self.performSegue(withIdentifier: "NextPage", sender: self)
    }

2 个解决方案

#1


0  

Firebase's task is a network task which normally runs on a background (lower priority) thread. But tableView.reloadData() is an UI task. So you probably need to execute UI related codes by following codes.

Firebase的任务是一个网络任务,通常在后台(优先级较低)的线程上运行。但tableView.reloadData()是一个UI任务。因此,您可能需要通过以下代码执行UI相关代码。

DispatchQueue.main.sync {
            let snap = child as! DataSnapshot
            let key = snap.key
            self.myList.append(key)
            self.CourseList.append(key)
            self.LessonsTableView.reloadData()
}

#2


0  

it is giving an error like this,

它给出了这样的错误,

    let userRef = self.ref.child("Users").child(Username).child("Schedule")
    userRef.observeSingleEvent(of: .value, with: { snapshot in
        DispatchQueue.main.sync {
            let snap = child as! DataSnapshot
            let key = snap.key
            self.myList.append(key)
            self.CourseList.append(key)
            self.LessonsTableView.reloadData()
        }
        UserDefaults.standard.set(self.CourseList, forKey: "LessonsArray")
        UserDefaults.standard.synchronize()
    })

推荐阅读
  • 本文介绍了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。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了PhysioNet网站提供的生理信号处理工具箱WFDB Toolbox for Matlab的安装和使用方法。通过下载并添加到Matlab路径中或直接在Matlab中输入相关内容,即可完成安装。该工具箱提供了一系列函数,可以方便地处理生理信号数据。详细的安装和使用方法可以参考本文内容。 ... [详细]
  • 解决Cydia数据库错误:could not open file /var/lib/dpkg/status 的方法
    本文介绍了解决iOS系统中Cydia数据库错误的方法。通过使用苹果电脑上的Impactor工具和NewTerm软件,以及ifunbox工具和终端命令,可以解决该问题。具体步骤包括下载所需工具、连接手机到电脑、安装NewTerm、下载ifunbox并注册Dropbox账号、下载并解压lib.zip文件、将lib文件夹拖入Books文件夹中,并将lib文件夹拷贝到/var/目录下。以上方法适用于已经越狱且出现Cydia数据库错误的iPhone手机。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • 这篇文章主要讲解了“如何应对Android面试”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何应对 ... [详细]
  • UIView的通用动画letviewUIView(frame:CGRectMake(10.0,10.0,100.0,40.0))self.view.addSubview(vie ... [详细]
  • swift中的uitable
    下面是一个静态的tableview于图可知有两个section头是11..和22..,其中222是一个tableviewcell!并且从图可知道样式是leftD ... [详细]
author-avatar
mobiledu2502930533
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有