[Music]
hello everyone so last time I showed you
how to fetch data from files door and
displayed in your Swift UI application
and you might recall that after we wrote
this piece of code I told you that it
was less than ideal why is that so first
of all it makes a number of assumptions
about the structure of our data that
we're fetching from file store so for
example here we assume that the title
app viewed is a string or that the pages
attribute is an integer so what happens
if we change our minds and change data
types in our file store documents or
what happens if we add new attributes to
our epheus to our documents or what
happens if we add new properties to our
swift classes or structs this code is
likely going to break also it is rather
verbose and you know a lot of lines of
code to write and that's always a bad
sign because less lines of code means
less mistakes that we can make so if
you've been around in the Swift
community for any length of time you
know that Apple has introduced the
codable API awhile ago in Swift 4 so the
codable API makes it a lot easier to map
data between your Swift classes or
structs and an external representation
such as a JSON file so you might have
been using the codable API for mapping
from JSON files to swift strikes the
good news is that fires door also
supports the codable API and today I'm
going to show you how to make use of
that so let's dive right in the first
thing that we need to do is to add
another pod to get support for this API
so firebase fire source work is the part
that we need and then let me just
you're on pod install and while we're
waiting for this to finish we can go
back into our IDE and start updating or
a model here to make it codable so the
first thing we need to do is basically
just say this is codable and usually it
that is enough so if all the attributes
match the attributes of your external
representation that's perfect but you
might recall that for a number of pages
we have a different attribute name in
firestorm let me show you so in here you
see we've got author we've got title but
it's called pages so what we need to do
is we need to tell Swift that where you
want to use different attribute name
here and we can use we can do that by
implementing the coding keys enum coding
keys and then specifying the attribute
names so we need to provide all the
attribute names here so first of all
okay is title okay it's author and these
are just the same so we don't change the
attribute names here but for a number of
pages it is pages actually so we provide
the name that we're using in our
documents so that's one thing and then
the other thing is what about this ID
attribute up here we don't have an ID
attribute in our document over there so
um but what we want to do is um so no ID
attribute here but we want to map our
document ID to the ID attribute in our
Swift struct here and to do that um we
will use a property wrapper that
firestore provides for us so firestore
swift
and then
[Music]
and use this document ID property
wrapper here make this optional and that
means whenever we map from a document
into our book struct firestore will read
the document ID of the document and map
it into this ID attribute here all right
so that's everything we needed to do in
our model now let's go into our view
model where we have this complicated
piece of code which performs the mapping
so what we want to do is instead of
doing all this make things easier and to
make it easier to let me import this
bundle here and then we can do this
query document snapshot data as and you
can see that this has decodable dot
protocol and what we need to do is we
need to provide the class of the struct
that we want to map into so book dot
self so this tells the codable support
we're using this class as a template
that we want to map into right so arm
and then less garthe's with a try and
then return this comment out this piece
of code here and we're using a compact
map so whenever we were trying nil this
gets filtered out by the compact map
right so this basically should be it let
me run this on the simulator and if
everything went well and I didn't screw
up we should see the same data in the
simulator as we did last time
and there we go so let me show you this
at the same time okay there we go
and just to prove that everything is
still working let me just change the
number of pages here mm-hmm and reduce
the number of pages and you can see that
it has updated straight away cool so
this is how you map data if you're
reading data so now what how would this
look like if we wanted to write data
into firestore and to show you how this
would look like I'm just going to
quickly implement a method for adding
new data so let me quickly do that so
first an ad book and it receives um a
book instance it's a book and then um it
actually is pretty straightforward and
so we already have our DB instance here
so it'll be a collection and we want to
write to the books collection and then
we want to add a new document document
and then you can see that we now have
the option to use from encode well
perfect because our book is uncodable
from book just like that and that's it
so the compiler will complain that this
might throw so we're going to do a
little try here that's one thing and
then it's also going to on that should
be fine
oh yeah for sure
we need to wrap this so do and then
catch this and then if there is an error
I will just print the error like this
and then yeah I was looking for this
so we'll just discard the result like
this right and this is how you store
data and to fire store pretty easy right
so that's basically it there are a
couple of things that um I would like to
show you before I let you go so first of
all let's go back here into our model so
we've been using the document ID
property wrapper and there are a few
other property records that are very
useful so let's go find out more about
them and to do so let's go into our
browser here and use the arm open source
search engine so this is search engine
and Google provides that you can use to
search the source code for all of these
projects here and the firebase SDK is
here as well and we want to search
within the iOS SDK so that's first start
by looking for the document idea wrapper
that we've been using before and you can
see here it says something about codable
so that's probably the right place for
us to start looking so this is the
source code for the document ID property
wrapper alright so what about the other
property records that I promised you so
you can see that this is a property
wrapper and then we'll just use that um
to search for all the other property
wrapper so there are three in total and
then we can look at the other one so
let's look at the server x term property
wrapper first so the documentation says
that
a type that can initialize itself from a
fire start I'm stem which makes it
suitable for use with the ad server
times and property record all right so
what does it mean so other times themes
are useful because the time on the
server and the time on the user's
devices will very likely not be in sync
so if you want to be sure that all your
data in your firestorm instance has the
same you know adheres to the same clock
essentially then you want to use server
time stems which is really important if
you if your application logic depends on
everything being in sync with one clock
instance right so and this is where you
want to use the server timestamp
property wrapper so for example you
would have an attribute on your Swift
class and then you would prefix that
with ad server times them and then it
would mean if your property in the Swift
struct is nil then at the time when it
gets written into fire store the server
ability plays nil with the current
server timestamp and then if you have
the same struct and it's already
prefilled with a triplet or the times
them and you write this back to the
server again because some other
attribute has changed the server will
not change the attribute which contains
the time step so this is pretty useful
if you want you if you want to do things
like I'm having a couple of attributes
that track the created date for a
document and the last updated date so
whenever you want to update the
timestamp just set it to nil and then
write it back and the severable update
it to the new timestamp there is a very
good article by dark Stevenson who goes
into detail about what he calls the
secrets of fire stores field Braille you
deserver time-stamped so I
and reading this article to get the
lowdown of how several times things work
like okay so let's go back and look at
the other property wrapper which is
explicit no and this one is useful if
you have an attribute that you want to
store back into file store and it is nil
on on the client side so in so if it is
nil and you want to write it back into
fire store and you want to make sure
that the attribute is written into fire
store if you have cases like that use
this property wrapper if you don't use
it and your attribute is nil and you
write the document back to fire store it
will not write the attribute so if you
want to make sure that that repeaters
written use this property wrapper all
right um that is these are the property
reference let's go back into our code
alright so that was it that was a very
quick introduction on how to use codable
and Firestar I encourage you to give it
a try
and use it in your applications so this
obviously also applies to regular
supported applications not just with UI
applications let us know how you like it
and if you run into any issues please
feel free to leave us a comment on our
github repository thanks for watching
and I will see you next time
[Music]