CloudKit: Saving to the Cloud | Swift 4, Xcode 9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody my name is Kyle Lee aka kilo loco from kilo loco calm and today we are going to be going over cloudkit more specifically learning how to save data to the cloud so saving to the cloud is what I called it whatever tools that we're going to be using today Xcode 9 beta 2 not necessary still use your Xcode 8 if that's what you're running we're going to be using Swift 4 but all the code is essentially the same so swift 3 is fine and obviously cloud kit this is a very straightforward tutorial in my opinion all you really need to know are the Swift language basics and just know how to follow along so let's get into it oh yeah alright so as usual we'll be starting off with a brand new project and let's get this going and we'll open up a new project and guess what we're going to do single View application that's right we're going to call it saving to the cloud alright call whatever you want but you should already know that alright so you know I'm going to store it in that KL Learning folder because that's where I learn you and I'm going to create it let's go ahead and get this sizing up bam bam alright so want to get our simulators up and running we don't have one so I'll get that running alright first thing we're going to go over to do is we're going to go over to the main storyboard I'm going to start laying out some of the storyboard stuff while the simulator is running so that you know we could kill two birds with one stone here alright so what I'm going to do is I'm going to just make it a your typical table view we're going to be saving to a table view and then we're going to also be you know just adding in data so all we really need is a nav bar and there's our simulator we can go ahead and stop that now want to put in our nav bar right navigation bar throw that in right there for some reason in the beta it doesn't go to the top if you change the size so I'm just going to leave it looking kind of funky hope that's alright with you because it's alright with me and we are going to add a bar button bar button item throw that item in there we're going to make it look like a little sexy plus sign so turn it to a little sexy ads on yeah I am sexy alright next we go over to the no no not navigation table view table you okay and you've guessed it guess what we're going to do don't pin it somebody needs a lot of attention so they put a huge muffler on their car and revving up all kinds of crazy all right so we're going to keep it very basic we're not going to do anything with the with the table view cells nothing too fancy we're just going to keep it as basic as we can get it so we'll run that make sure that that's coming up and while that's working we'll move over here to our view controller alright so we have our table view okay here it comes there we go see looking pretty sure schmancy well you see that glitch who I was kind of weird huh all right so what we're going to do is the way that we're going to add stuff in here is going to be through a UI alert controller so we got to have actions for that and then we also need to be able to reference the table view as well I think but we'll get there when we get there first let's get going with the action so app IB action will do a week didn't spell that right so let's make sure that's a capital A we bar and we'll call this oh no no what am i doing ib action func right on um plus tapped okay so when we tap that plus what should happen little alert controller should come up right all right so let alert equal UI alert controller I'm going to start this up initialize with the title okay say type something and we'll just say that we're going to be saving notes right so what would you like to save in a note and the preferred style you guessed it alert style baby alright we want to have an input field on the alert right so what we'll do is we'll do alert if you'd know how to spell Oh Kyle why are you doing this you're doing it too small there we go sorry for those of you that are on the phone you know that I usually catch it maybe not right away but I usually catch it somewhere in the video all right so we're going to add a text field bam-bam-bam field all right so now in the add text field we have access to that text field and the properties that we want to manage with it so only thing we'll do is we'll change the placeholder for the text field so let's do that so text field dot placeholder is going to be equal to type note here sure why not okay so now we have an input field now we need buttons in order to control the actions once the user you know does in clip right so we obviously need to need a cancel actions will cancel oh no what let cancel equal UI alert action all right and the title will be you guessed it cancel style will be you guessed it cancel and the handler will be you guessed it now yep let we'll say post write equal UI alert action we're just going to do the same old thing right right right right get this out the way it makes it look better there we go doesn't that look so much nicer it sure is how all right I don't want to have to fill this all out by myself so we're going to do that title will be paying it doing it post style will be default handler will be won't need access to the actual action so we could just put it under score right there well will happen on the post will be when we're actually trying to post something into the cloud to cloud kit so we're going to leave this as default for right now we'll just say print here for the time being actually you know what we need to access our alert text field so will will actually print whatever's in the text field right so guard let text equal alert dot text fields dot first dot text else return so if there is text we'll just go ahead and print that all right so now what we have to do is add those actions to the alert so alert dot add action will add our cancel and alert add action and we'll add our post yeah and then all we have to do is what are we in a closure we don't need so yeah we don't need so we just have to present that view controller which is our alert and animate it of course because we like pretty things good hello okay so now what we have so far is just whenever we press post you know it will print out we have an input field that's cool we also need to get these extensions going for the table view though so let's go ahead and get that going we're just doing all the setup right now nothing wrong we do in the setup got to do the setup it's part of the project all right right table few data source data source there it is alright so number of sections BAM return one let's get something let's get some rows down here okay number of rows return zero for right now and sell for row at index path will do let sell equal UI tableview cell the default cell will just return that cell and then we also want to say we're going to eventually modify this cell to have texting it right so we might as well just do that right now so cell dot text label dot text is equal to just a blank string for right now all right so we have everything set up we're going to want to make sure that we have our data source connected to our table view and I think that will be it for the main set up real simple stuff here guys so data source right and then we want to make sure that our action is connected as well we need access to the table view outlet so let's go ahead and make that as well so at IB outlet we our table view because we got to reload that name you want able view there it is connect it let's connect it alrighty now where are you if I could find it if I can find it there it is and we're all connected all right let's run that make sure that there's nothing wrong should be good so far so then doing it into crazy it we're just going to get ready to you know get this whole adding stuff to the cloud going when we do this bring something up we'll say blah blah we'll say get out the way so that we can see stuff and we'll say post and we got blah blah so looks like everything is working perfect now comes the part that you actually click on the video for cloudkit alright so how do we get set up well first thing you want to do go over here capability capabilities going to go to iCloud you can turn that on right key value storage yes please cloud kit yes please we're going to use default containers yes we are and we're going to jump over to the cloud kit dashboard going to open that up mm-hmm just let it wait you're going to sign in BAM you're going to get a notification allow you to just type in the same password that's right there on your screen interesting 2-step verification if you ask me but okay by all means I'm going to say done because we're done going to continue as you would expect in a way for cloud kit or the the cloud dashboard to open up it's going to look a little bit different if you've done this before you're going to go over to whatever bundle ID you would expect it to be so what do we call ours we called it saving to the cloud God look for that bundle ID no no yes click it BAM alright we're in development you're going to play in development so we're going to go over to data yes we are so now you can see all the data that you have access to you you can see your record types indexes subscriptions this is for notifications and stuff and security roles and stuff like that now by default there's automatically a user object in here which you can use so if you're just going to if you're just interested in creating users you can do that you can look at who's authenticated you could give them you give them their permissions whether they could read whether they can write say who the Creator is they can write they can't read stuff like that we're going to be not going to be dealing with this too much we're going to be making our own object which you most likely will be and once you know how to make your own object managing this will all be the same essentially so we're not going to start with anything in here just yet but we're about to get to it in the code so when you make it in the code and you actually post it everything will be initialized up in the cloud from that first post yeah well you'll see how it changes let's let's get into it let's take a look at it alright so this is where we want to you know what let's let's extract that function and just you know put it somewhere else so func save to cloud right and we'll say note and this note will be a string right because we're going to take the text we're going to pass it in into here and then it's going to save that string right so what we want to make sure is obviously that you're your capabilities you have it enabled ripe it should have made this entitlements for you if it didn't try on clicking it and then re clicking it but you need this entitlements for it to work properly and then last but not least you need to import cloudkit BAM all done alright so when we're going to save to the cloud first thing that we have to do is we have to say we have to specify what kind of record that we're going to save it's going to be like this is essentially like the user's is a record it's kind of like an object it's similar to an object and we have to we have to save something to that object that's up in the cloud so we want to save a note so what we're going to do let me take a look at my notes to tell you about how to save notes so I can make sure I show you guys this correctly first thing we want to do is want to make that note so or that record so let new note equal seek a record right and then we're going to initialize this with a record type of type string now you can get really into it you can specify what kind of like record IDs and this will help you segregate your data from from itself like if you you want to be extra cautious and the way that you store each of your objects in the cloud but we're going to keep it really simple today and we're just going to go with the record type man I keep doing that double quotes thing okay so what we want to call it is note that's going to be well it's it's the same thing as user like how we saw it back in the cloud kit dashboard ours is going to be called a note so we want one of those from the new note or we'll just call it note I don't know why I have it as new note what note and then we'll do note dot save or not foldable is it no dot set value so we have to set value because remember that cloud kit it only takes the main types of data values like if you worked with core data before it's only the main types of data values you can't use swift objects at least from my understanding I've tried it and it's giving me errors I don't know if I did it the wrong way but you from when I tried it you can't save Swift objects you have to save like strings integers you know boolean things like that so the value will be text right or the note that we want to save oh that's what I called it new note because this one's called note so new note right we don't want to have any overloading or any weird errors going on so it will have new note and then we'll pass in our note which is that string right here for key the key that we're going to save it as is going to be we'll just call it content you can call it whatever you want you just have to remember remember that you're going to be referencing this so make sure you choose a key that you would remember you can call it like payload response you call it data I've seen you know whatever you want to call it but you just have to set a value just have to remember to keep that okay from there we're going to need to frequent frequently reference our database so what we'll do is we'll do let database equal CK container dot default dot and this is where you choose what kind of arm this is where you choose what kind of database you want to use now you're going to want to say either public shared or private now um this is it depends on what the object that you're going to be using is and how you're going to be using it if you want other users to be able to access this data like they won't it's not necessarily like hacking it's like um you know think about it like Twitter right if you want people to be able to see other people's tweets then you'd make it public because everybody wants to see those tweets right but if you do like private private is more like like your own personal stuff like if you were keeping a diary type of thing right and shared is um shared is like I from my understanding it's you can specify which users you can see it but it's not completely open to everybody right so only like your friends can see it or something like that you can specify the rules but since we're going to only be taking notes and you don't want other people to see your notes we're going to go with private you guys do whatever you want I'm going private ok so now we have a reference to our CK container our private database and with that what we'll do is all we'll do is say database dot Save and we're going to save what do we have I mean you can see which ones you can see what you can save you could save a subscription which is like for push notifications you can save a CK records own which like once again it's a way to break up the different objects that you have stored in the cloud but we're just going to save this simple record that we have called new note so we're going to save a record pass in our new note and completion handler you guys already know me right record right but error cow doesn't do error handling in this videos you member so remember that they usually come back as optional so records optional so we'll do a guard let record eat little pups paw equals equal record else return all right so now we have a record and if we have a record then we're not going to really do anything with it there's a few things you can do you can kind of like look at it and see like things like this creation date create creator user record ID things like that you can access some of these properties if you want and that would be to you know make your app more remote robust but we just wanted to make sure that it wasn't equal to nil so what I'm actually going to do is do this is not equal to nil right and if it's not equal to nil then that means that we were able to successfully save that record card record no I'm right you're wrong all right no no actually I'm wrong my dad you were right Xcode my bad if we were able to if if we are actually able to successfully save a record then this will be this will actually have a value therefore won't be nil and therefore we can just print out saved record with note and I have to actually put this in parentheses and we'll say record dot [Music] object for key and then we'll just say content right I mean essentially you could do that I'm just going to put that should be fine whatever or you know what that's just too much we'll just say save record right so let's go ahead and try this out as you can see it's not that much I mean realistically the cloud the cloud kit stuff that I've done so far what one line there two lines they're like six lines here real simple stuff this is my note this is my note and we were not able to save so we have to go back and look what's happened so let's go ahead and print our error if any and find out hey Kyle what happened oh well that's probably what happened Kyle you so dumb didn't pass it into the the dang thing guys why didn't you tell me oh you did tell me you just weren't listening Kyle oh my bad my bad guys I didn't mean to I didn't mean to ignore you got to call the function if you want to use the function of like man it's real simple how did I miss something up something so simple up alrighty so we're up and running this is a different note post-it [Music] nothing happened why didn't nothing happen oh okay so let's see what the error was print error what did you do Kyle why did you break it like man I don't know okay so save the text BAM or saving set value for key and then we're saving that save something man optional not authenticate it this request requires an authenticated account all right so since we're not authenticated right so let's go ahead refresh this bad boy I should be oh you know what I might not be signed in so I didn't sign in that's why I'm not authenticated so let's go ahead and do that okay so I had to skip so that you guys couldn't see my password but yeah I wasn't signed in so obviously wasn't working so we have to go in and make sure that everything is all signed in and squared away this new keyboard isn't so fancy all right sign-in into iCloud don't merge no no no no we're not doing all that today all right there it goes guys so make sure that you are signed into iCloud because you do have to have iCloud account or a meet account or whatever you want to call it you have to have that at me or at iCloud account to use iCloud slash cloud kit so now when we go back to saving go back to our app and then let's make sure that we're still running so we'll go ahead and restart the simulator make sure that everything's good and golden it's a good thing that you guys saw that because um you know you want to gotta know these things you wouldn't know these things off the bat if I didn't tell you maybe or maybe you look maybe maybe you would I don't know alright we're going to save let's see what happens are we going to get it error are we going to get safe so save record BAM okay so now we go back over to our records right nothing right here got a refresh BAM your refresh and let's see what happens if your internet works we'll see all right I finally reloaded I don't know why it took so long that was kind of weird all right so now now since I'm using this device I can see any private records that are for me under this account which this account is tied to my Apple account right so when I do query as you can see it already knows note right users and note and if you want to see the record types you can see user which is a default type and then our note this wasn't here before I think also in security roles it'll show note see that wasn't there before I made that so let's go back to records and what we're going to do is we're going to query we want to we want to query in the database and see what we got in there so query records there's a problem querying the note type field record name is not marked queryable enable the queryable index on the fields in indexes section well it tells you exactly what to do guys go to indexes right we have to make sure that we go to note right we're going to add an index we're going to open this little drop-down up record name make sure it's queryable you can chant it you can't well you can't change some of them have different options like like the content you see how it has queryable searchable sorted by sortable you can access all these different things right now we're just going to do record name so we'll add that oh no no no we want to add another one pom sorry it's already added right here but make sure that you do save record type go back over to records now when we do query record should work all right look at that BAM um content maybe you would hey that's what I said yeah we got it okay so now that we're saving right now that we got a little little potion apostrophes in there we're saving now what are we going to do well we have to actually be able to read load that data and it takes a little while it's not as fast as some of these other services that are out there now that we're saving we have to be able to query from the app we don't want to just query from back-end right that makes no sense for us or maybe it does I don't know but we want to be able to query so what we'll do is we'll create a different function called query funk funk you up uptown funk you up so query database bang right and what do we want to do when we query the database well we have to make a query so let query equal seek a guess what query and we are going to initialize this with a record type a and so if you remember record type that we want to access is note the one that we made or if you wanted to do users or you know whatever you saved you just type it right on in there but we want to query the note and for the predicate we'll just do NS predicate you press ENTER on that you're going to initialize this with a a boolean you're just going to say true right that just means that in this predicate no I don't really fully understand it to be honest so I'm not going to be su right but the way that I understand it it's a way to like it's not necessarily like a sort but it's a way to specify how you want to access the data kind of and all we're saying is that from my understanding all we're saying is that note that it has a record type of note so we're just getting everything back from our private database that has a record type of note so you're getting back everything right right so now we have to do database our database right and we have to perform a query so what we'll do is we'll pass in our query we're not segregating our data or our records so we'll just pass in nil for the zone and then for the completion handler we have CK an array of CK record right so that that will just be called records and you guys already know Kyle don't do it it doesn't error-handling his tutorial so guard let's Records equal records else returned alright so now we have our records now we actually have to pull the data out of those records so what do we have to do let's see so we want to access these records and then yeah we want to put them in a different array so that our table view can access them right so let's just call them notes we'll put it up here right underneath our database we'll have our call notes and this is just going to be an array of strings and then we'll be able to access our records get the the notes and yeah actually we'll what we'll do is we'll do it as CK records CK record right this way we could just pass in the record save them and then we'll be able to access everything that's in that record so essentially would be acting like an object a data model right so we'll do self dot notes is equal to records right and all this is done asynchronously so that means that we're not on the main thread so when we call refresh we have to make sure that we do dispatch so dispatch Q dot main dot async and then we're going to do self dot table view dot reload data right because now we're going to what we're going to do is we're going to do let these split that wrong we're going to let these notes populate the data in here so now that we have an array we could do notes count which makes sense right and then what we can do here is we'll do notes index path row dot dot value for key this is already getting ugly I'm actually put in something else so we'll call the key remember we had to ask content make sure you spell it right or else it won't be anything as string and we'll just call this a note actually so let note equal this feeling kind of crazy today force unwrapping it's all good so good okay so now we don't need this get out of here who checks for errors anyways right okay so now we're saving our notes up here I am right or yeah whatever is in there we're saving up here and then we're also reloading the data so that this will be updated and this will be updated so now all we have to do is query the database eventually right so we'll do that on viewdidload query database let's go ahead and try that query out let's see what happens guys should work might not you know Kyle full of errors okay oh well hey look at that now that you're saving alright but one last problem one last problem when you post nothing happens right so what we'll do is when we save something well also query only problem with doing this is that that cloud kit isn't super fast so this may not work well I can try here and show you in the tutorial I just want to show you it right here because um it'll give you idea of if this is a good practice or not it generally isn't because it takes a little bit took a little bit of time for a cloud kit to update but let's try it let's try it so post it see it pulls but it's it's trying to do it so quickly that nothing happens so if I do it again watch see how it added it let's try it right here but the other one wasn't added that's because it takes a little bit of time for a cloud kit to update so what we can do and this is not necessarily the best thing to do but we'll just do a pull to refresh on this table view and that will allow us to that will at least see if our table view is updating so in our table we'll do let refresh control equal UI refresh control and we'll do refresh control dot attributed title is equal to is equal to oh wait it's attributed to and it's attributed string and we want to do it with a string and we'll just say cool to refresh and then self dot table view dot refresh control is equal to refresh control oh and we also have to add a target uh or else it's not going to work Oh add target self selector want to do weari database and then value changed okay so I hope that makes sense we're just adding a refresh control so that we can pull to refresh and then look at it what's going on right here hashtags lector not exposed to okay it's interesting I guess you need this objective-c so this is the swift for part I guess all right let's do this so if three didn't have to do this Swift for they just want to make sure that it's safe abject to see bridging blah blah blah stuff like that all right so did I take it out of the other spot when we added it yet to get out so yeah I took it out BAM saved it pulled to refresh takes a little while mm-hmm oh you know what did we even do it oh well first we have to do self dot table view dot refresh control dot stop um animating what is I forgot what it was begin refresh and refresh there it is so make sure that we can actually have it go back up when it's finished updating right this time we'll just put in numbers so that it's easy to see which one it is so let's do this and we'll post all right saved record pull the Refresh BAM right there there it is alright so one of the things that you might have noticed is that it doesn't put them in order necessarily see how it goes in the middle so what we have to do is we actually have to sort them and we do that right here where is it where we actually do the query so what we'll do is we'll do let sorted records equal or no wait we'll do it with yeah sorted records equal um records dot sort and then we'll just say and remember that each record has each record has a creation date so dot creation date creation date so um see what's going on yeah so now when we do it oh no no no no hold on sorted records all right so now when we do it I believe it will be in the correct order produces I don't know I don't know what that was but it worked ok so now we'll get the most recent one up top so the most recent one will be the greater date and the less recent one will be like the older one will be the lesser date so hello there and now when I pull to refresh it should be at the top see if I wanted to go in the other way then you do it that way in yeah and that's all you really have to do save and stuff guys it's pretty easy it's pretty pretty easy lemon squeezy easy P out we'll remember that it's kind of slow but yeah pretty easy all right guys that's pretty much it last thing I will do is I haven't done this before but let's try to save this on github right from the navbar or whatever this thing is called was it called see you don't have to know all this stuff ya navigation navigator the navigator it might not work so while that well that's doing whatever the heck is doing let's go over to github let's do it taking a while know why it's taking so long didn't take that long at WWDC and I did it look at all the stuff working like normal which is good we just want the source code source editor or whatever it's called all right it doesn't look like it's going to work this time guys we'll just do a new repository like normal make sure that you guys have access to this stuff cloud kit saving - to the cloud BAM this is the code from my cloud kit tutorial alright guys that's pretty much it I hope that made sense I hope that you got to see the new cloudkit dashboard or iCloud dashboard whatever they're calling it now and I hope that made sense I know that I kind of dragged it out kind of do stuff takes a little bit longer than most people but you know if you're a beginner you can do this now see I think that it provides you with a valuable skill because maybe the company that you work for wants to use cloud kit it's a little bit slow in my personal opinion I'd prefer to go to firebase that's me but you know sometimes you want to just stay an apple and I completely understand that I'm an Apple fanboy myself and I wanted to just use everything that's Apple but sometimes you know things that Apple puts out they aren't the best a cloud kit what did I call it saving to to the cloud okay so get in it get add are no promote add origin get cool origin/master enter in yo password get pull gonna get I already pulled it right at all git commit am finished saving to that cloud also another reason why you would want to use cloud kit is because it's cheaper than other ones when you start scaling I believe actually have to relook at those pricing actually scratch that because I haven't looked if they change the pricing structure on it but it used to be cheaper than most other services when you start scaling now there are more limitations though so just keep that in mind so kid a push origin master did already it already push Oranje master and then we put in our past WOD and it's pushing it's pushing and it's pushed and we refresh BAM right there available for you guys alright so if you have any questions feel free to leave them in the comments this thing's still not working out know why pretty simple pretty straightforward in my personal opinion and yeah reach out to me on Twitter reach out to me on YouTube let me know what type of videos you want to do every weekend I do one video of something that I know and one video that you request and it may it may take a little while to get to your video but I promise that we'll get to and I'll do a long detailed discussion like this and hopefully it'll be step-by-step so that you can follow it implemented in your project or whatever you need to do and break down some of the pieces that go along with it so if that's the type of style that you like learning from then yeah if not you can check on my youtube page I have links to all the other not all of them of the other Swift YouTube channels other developers that are you know they they have a different style than I do and maybe that style will fit you more so go ahead and check them out - they're great they're great developers - I think that's all I got for you guys I have something special for you guys coming out in the middle of July so make sure that you guys subscribe to this channel so that you can be up-to-date with what I plan on putting out there for you guys I think it'll be you guys are asking for a couple of different things I've been listening to you I read every comment that I get if I don't get back to you right away I'm sorry I just get busy with the family and everything but yeah but I think that's all I got to say for you guys check out the slack link I think it's finally automated if not then you know cinema's to me and I'll try to get you in all right that's all that was a cloud kit saving data to the cloud hope you enjoyed subscribe like all that good stuff and as always make sure that you are always coding passionately yeah there you go bye
Info
Channel: Kilo Loco
Views: 21,678
Rating: undefined out of 5
Keywords: swift mbaas, swift baas, how to use firebase ios, swift 4 cloudkit, ios cloudkit, swift saving to the cloud, swift backend, cloudkit for beginners, how to use cloudkit, cloudkit database, cloudkit read data, cloudkit write data, swift cloudkit data model, how to create a cloudkit database, swift ckquery, swift ckrecord, cloudkit dashboard, cloudkit ckrecordzone
Id: 2Y45vk7d_Bg
Channel Id: undefined
Length: 54min 42sec (3282 seconds)
Published: Mon Jun 26 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.