firebase使用_使用SwiftUI和Firebase构建我的第一个应用程序

firebase使用

Last week I released my first app on the App Store called Co-shop

上周,我在App Store上发布了我的第一个应用程序,名为Co-shop

I developed the app in conjuction with the Covid-19 pandemic. The aim was to help people who feel most isolated get their shopping essentials by asking their local community.

我与Covid-19大流行一起开发了该应用程序。 目的是通过询问当地社区来帮助那些最孤立的人获得购物必需品。

The app was really fun to make and I developed it using Apple’s relatively new SwiftUI framework along with Google’s Firebase for the backend.

该应用程序的制作过程非常有趣,我使用苹果公司相对较新的SwiftUI框架以及Google的Firebase后端进行了开发。

I’m writing this article to share my initial thoughts on the two as a new comer to mobile app development. If you are reading this a fellow new comer, I recommend both highly as a great starting point for app development.

我正在写这篇文章,以分享我对这两者的初步想法,这是移动应用程序开发的新手。 如果您是新手,那么我强烈建议您将两者作为应用程序开发的良好起点。

SwiftUI (SwiftUI)

For the last 5 years or so, creating a mobile app is something that I have always wanted to do. In particular on iOS mainly because thats what I interract with the most.

在过去的5年左右的时间里,创建移动应用程序一直是我一直想做的事情。 特别是在iOS上,主要是因为这就是我最感兴趣的部分。

However, I previously found that I would embark on a large set of Swift tutorials, come out on the other side having remembered nothing, or more likely lost complete interest. This was mainly my fault as I find starting a project and learning as I go along far more beneficial than following a tutorial. I grew tired of looking at the Interface Builder, fiddling with constraints and Xcode in general felt quite intimidating. Nevertheless, I was no closer to creating an app.

但是,我以前发现我会着手进行大量的Swift教程,而另一方面却一无所获,或者更有可能失去兴趣。 这主要是我的错,因为我发现开始一个项目并在学习过程中比学习教程更有益。 我对看接口构建器感到厌倦,对约束和代码一无所知,总的来说,它非常令人生畏。 但是,我离创建应用程序还很近。

And then came SwiftUI…

然后是SwiftUI ...

When it was announced at last years WWDC conference along with the refreshed Xcode 11, it was clear that iOS development was something I ought to try again. I was impressed initially by the live previews, the new security features but most of all how simple everything looked, it hardly seemed like you needed programming experience.

当它在去年的WWDC会议上与更新的Xcode 11一起宣布时,很明显,iOS开发是我应该再次尝试的事情。 最初的实时预览,新的安全功能给我留下了深刻的印象,但最重要的是,一切看起来都很简单,似乎几乎不需要您具备编程经验。

Here are some of the pros and cons I found whilst trying out SwiftUI for the first time:

这是我第一次尝试SwiftUI时发现的一些优缺点:

优点 (Pros)

  • Simple and intuitive - As someone who has mostly done backend development to date, I found the declarative approach of SwiftUI to be somewhat familiar and incredibly easy to pick up.

    简单直观-到目前为止,作为主要从事后端开发的人,我发现SwiftUI的声明性方法有些熟悉,并且非常容易上手。

  • Looks great - Creating a UI that looks like an in house Apple application is incredibly easy and takes a few lines of code. I have never thought as myself to be particularly gifted as a UX designer so after a few days playing around I felt pretty confident Co-shop was a project I could bring to life.

    看起来很棒-创建一个看起来像内部Apple应用程序的UI非常简单,并且需要几行代码。 我从来没有想过自己作为UX设计师特别有天赋,所以在玩了几天之后,我对Co-shop是我可以实现的项目充满信心。

  • No more constraints! - I’m sure I can’t be the only one to say that constraints were not fun to deal with. With SwiftUI layout really is a dream to work with and the resuability of views ensures different parts of your app look consistent with one another. Most of the layouts I made were handled with some sort of Stack and Spacer which are very easy to get the hang of.

    没有更多限制! -我敢肯定我不是唯一一个说约束不好玩的人。 使用SwiftUI布局确实是一个梦想,并且视图的可重用性确保应用程序的不同部分看起来彼此一致。 我制作的大多数布局都是使用某种Stack and Spacer处理的 ,这些东西很容易掌握。

  • Online resources - Seeing as I decided to leave Swift tutorial classes for this project most of what I created I learnt from online resources. Seeing as it’s a new framework, all resources are up to date which makes a nice change. There are cases where I found my problem to be too niche and had to find a work around but in general solutions were easy to find. If I was to recommend one resource it would be Hacking with Swift. There’s loads of clear, up to date tutorials, and as I m a sucker for anything written in markdown, I welcomed it with open arms and wasn’t disappointed.

    在线资源-当我决定离开该项目的Swift教程课程时,我从在线资源中学到的大部分知识都将留给该项目。 看到它是一个新框架,所有资源都是最新的,因此进行了很好的更改。 在某些情况下,我发现自己的问题过于小众,不得不寻找解决方法,但总的来说,解决方案很容易找到。 如果我要推荐一种资源,那就是Swift的Hacking 。 这里有大量清晰,最新的教程,当我沉迷于用markdown编写的任何内容时,我张开双臂欢迎它,并不感到失望。

缺点 (Cons)

  • iOS13 or later - This is probably the worst part of using SwiftUI because using it does limit your audience. Even though iPhone users do tend to update their phones more often then Android, it’s not ideal but something I’ve accepted.

    iOS13或更高版本-这可能是使用SwiftUI的最糟糕的部分,因为使用它确实会限制您的听众。 尽管iPhone用户确实比Android更新频率更高,但这并不理想,但我已经接受了。

  • Inconsistencies - During development I found that there were cases where updating @State variables in certain cases were not refreshing the view as expected. At the start, I thought this was my own user error however after researching more it seems to have happened to others, so If it is a bug I’m sure in the next iterations of the framework it will be ironed out.

    不一致-在开发过程中,我发现在某些情况下更新@State变量有时无法按预期刷新视图。 开始时,我以为这是我自己的用户错误,但是在进行了更多研究之后,其他人似乎已经发生过,因此,如果我确定是一个错误,我相信在该框架的下一个迭代中它将被解决。

  • General Xcode annoyance - ..goes without saying however it has improved.

    一般的Xcode烦恼- ..不用说,但是它已经得到了改善。

火力基地 (Firebase)

I heard about Firebase initially through tutorials I had done prior to SwiftUI coming out and chose it at as my backend solely for it being somewhat familiar to me and for my purpose, completely free. The Getting Started steps are well documented and there are lots of tutorials online to help.

我最初是通过在SwiftUI发行之前完成的教程听说过Firebase的,然后选择它作为我的后端,完全是因为它对我有些熟悉,并且完全免费。 《入门指南》的步骤已得到很好的记录,在线上有很多教程可为您提供帮助。

The amount of tools Firebase gives you for free are insane. Having not cast too much thought into what functionality I may need for my backend service when first starting out, I was pleasantly surprised with what is provided.

Firebase免费提供给您的大量工具是疯狂的。 刚开始时,我并没有过多考虑我后端服务可能需要的功能,但是我对所提供的功能感到惊讶。

Here are my thoughts on the features I used:

这是我对使用的功能的看法:

认证方式 (Authentication)

Firebase makes authentication really simple. It handles lots of scenarios out of the box such as:

Firebase使身份验证非常简单。 它开箱即用地处理许多方案,例如:

  • Anonymous sign in

    匿名登录
  • Password length constraints

    密码长度限制
  • Ensuring unique/valid email addresses

    确保唯一/有效的电子邮件地址
  • Adding a display name to users

    为用户添加显示名称
  • User email verification and password reset emails

    用户电子邮件验证和密码重置电子邮件

There is also lots of documentation to add third party authentication services, in particular Sign In with Apple, which lots of end users appreciate for the security aspects.

也有很多文档来添加第三方身份验证服务,尤其是“使用Apple登录”,许多最终用户出于安全方面的考虑而赞赏。

The only downfall I found was that Firebase by default does not allow username sign in, and to implement requires a bit of work. You have to store each username in Firestore as a document, with a field containing the mapped UID of the user. Then on sign up, check that the username does not already exist by querying the database. I ended up not implementing it as a feature, mainly because of the extra effort, and the majority of users will want to sign in with a third party service anyway. I do hope to see it as a feature in the future though.

我发现的唯一缺点是,默认情况下,Firebase不允许用户名登录,要实现该功能需要一些工作。 您必须将每个用户名作为文档存储在Firestore中,其字段包含用户的映射UID。 然后在注册时,通过查询数据库来检查用户名是否不存在。 我最终没有将其实现为功能,主要是因为付出了额外的努力,并且无论如何,大多数用户还是希望使用第三方服务登录。 我确实希望将来将其视为功能。

消防处 (Firestore)

With Firebase you get two options for a realtime database, Firebase and Firestore. I chose the latter as it was new and shiny at the time, and seemed like what most people were using.

使用Firebase,您可以获得实时数据库的两个选项:Firebase和Firestore。 我选择后者,是因为它当时很新而且很有光泽,而且看起来就像大多数人使用的一样。

Firestore stores data in documents and collections rather than a JSON tree structure which felt a bit more intuitive to use. Running queries felt very quick and attaching listeners were really easy to get live updates in my app. Although I didn’t have a lot of data to play with, I liked the idea of how you can partition documents into further sub collections to limit expensive queries.

Firestore将数据存储在文档和集合中,而不是使用感觉更直观的JSON树结构。 运行查询的感觉非常快,附加侦听器真的很容易在我的应用程序中获得实时更新。 尽管我没有很多数据可玩,但是我喜欢这样的想法,即如何将文档划分为更多的子集合以限制昂贵的查询。

One thing Firestone does not have, compared to Firebase, is GeoFire. This gives you the capability make geo queries i.e to add a location to documents in the database so you can listen for all nearby users within a given radius. Since my app needed this feature I had to use an open source third party called GeoFirestore to be able to do what I want, which wasn’t ideal due to the lack of support for Swift 5 and it not being as reliable. If I was to do the project again, I would probably choose Firebase for GeoFire and live with the flaws it brings.

与Firebase相比,Firestone没有的一件事是GeoFire。 这使您能够进行地理查询,即向数据库中的文档添加位置,以便您可以侦听给定半径内的所有附近用户 。 由于我的应用程序需要此功能,因此我不得不使用名为GeoFirestore的开源第三方来执行我想要的操作,由于缺少对Swift 5的支持并且它不那么可靠,因此这不是理想的选择。 如果要再次执行该项目,我可能会选择Firebase for GeoFire并忍受它带来的缺陷。

云功能 (Cloud Functions)

This was my first experience with Cloud Functions and they really impressed me. To have serverless code executed on the fly is incredibly helpful. In particular I have been able to:

这是我对Cloud Functions的第一次体验,它们确实给我留下了深刻的印象。 快速执行无服务器代码非常有帮助。 我尤其能够:

  • Make changes to my app without having to publish an update to the App Store, this meant I could propagate changes instantly to the end user.

    对我的应用程序进行更改,而不必将更新发布到App Store,这意味着我可以立即将更改传播给最终用户。
  • Tie Authentication triggers with Firestone updates. This can be really helpful to create documents for a new user on sign up.

    领带身份验证通过Firestone更新触发。 这对于在注册时为新用户创建文档非常有帮助。
  • Trigger cloud messaging tasks to send notifications on updates of the database.

    触发云消息传递任务以发送有关数据库更新的通知。

Overall it’s allowed me to keep my app incredibly lightweight, focus more on SwiftUI and therefore sped up my development dramatically. Functions have much more to offer and I hope to play with them more in the future.

总的来说,它使我能够保持我的应用程序异常轻巧,更加专注于SwiftUI,因此极大地加快了我的开发速度。 功能还有更多提供,我希望将来能更多地使用它们。

云消息传递 (Cloud Messaging)

I left figuring out notifications of my app right till the end of developing the app and it went very smoothly.

我一直在弄清楚我的应用程序的通知,直到开发结束为止,并且运行非常顺利。

Once you have configured your app to listen for remote notifications, the Firebase console provides a good on boarding tutorial and the ability to send test messages to your devices.

将您的应用程序配置为侦听远程通知后,Firebase控制台将提供良好的入门教程以及向您的设备发送测试消息的功能。

You can’t send notifications from your app which makes sense so I dealt with them in cloud functions and it was very simple to implement. You have free rein to customise the title, message, sound and badge number of a notification. The badge number is helpful as it sets the app icon with the red notification circle on the phones home screen.

您无法从您的应用程序发送通知,这很有意义,因此我在云功能中进行了处理,实现起来非常简单。 您可以自由控制自定义通知的标题,消息,声音和徽章编号。 徽章编号很有用,因为它会在手机主屏幕上用红色通知圆圈设置应用程序图标。

Overall, I haven’t had a bad experience with Cloud Messaging since enabling it, it’s another great tool in the Firebase arsenal.

总体而言,自启用以来,我对Cloud Messaging的使用从未有过糟糕的经历,它是Firebase军火库中的另一个出色工具。

Those were some of my initial thoughts after developing my first app on iOS, I’m sure my opinions will change on some things as I continue to develop!

这些是我在iOS上开发我的第一个应用程序后的最初想法,我相信随着我的不断发展,我的观点会有所改变!

Thanks for reading! 🙂 If you have any questions or want to share your experiences with either, post a comment below!

谢谢阅读! 🙂如果您有任何疑问或想与他们分享经验,请在下面发表评论!

I also have a newsletter which is completely free where I write about swift related topics. Feel free to sign up using the link below! 👇

我也有一个通讯,它是免费的,我在其中撰写有关快速相关主题的文章。 欢迎使用下面的链接进行注册! 👇

翻译自: https://medium.com/swlh/building-my-first-app-with-swiftui-and-firebase-56ad5764b881

firebase使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值