The course is part of this learning path
This course focuses on a concept known as core data, which will allow us to store our data in a database. We'll then show you how to apply this concept to a real-life app which we will build called Art Book.
Learn how to create a local database allowing users to save data on their phones using core data
This course is intended for beginners who want to learn how to build apps using Swift.
To the most out of this course, you should have some basic understanding of programming and computer science in general.
Hi, within this lecture, we're going to see how we can send messages to the app from one ViewController to another or from one ViewController to the whole app so that we can trigger some functions, trigger some actions. And, right now, we have some problems. Even though we can add a new picture, new art to our paintings, we cannot go back to the ViewController. And, in order to do that, there is a simple line of code. I can come over to 'DetailsVC' and I can just say, 'self.navigationController?.popViewController(animated: true)', okay? And it will just take us back to the original ViewController, okay. And we can actually try this right now. It won't fix our newly added art not showing on the tableView problems. And that's why we're going to learn Notification Center because bringing us back won't help this. So, let me go to 'Image' and add one so you would understand much better what I'm talking about. So, let me choose these flowers and I'm going to call these 'Flowers'. Okay, the artist? I'm going to go for 'Edward Munch'. So, I believe not Mumcu, Munch, okay? So, I believe I'm very bad at arts. So, let's 'Save' this. And as you can see, we got the ViewController here. But, we cannot see the newly created painting. So, what we're going to do? We're going to have to send a message from the DetailsVC to this ViewController saying that this new data has been added to the CoreData. Please go and take it. And in order to do that, we're going to use something called Notification Center. So, Notification Center actually let's us broadcast messages in our app so that we can observe them anyway. So, if you write 'NotificationCenter', as you can see, this is a mechanism that enables us to broadcast information for registered users. And we don't have any registered users right now, registered observers. We're going to add an observer to our ViewController later on. First, we need to send this message and then we need to find a way to get this message in the ViewController. In order to send this, I'm going to go for 'default.post', okay? So, if you say, 'post', it will display some options for you. We're going to go for this one: name and the object. So, we're not even going to use an object. We're not going to send any object. We just need a name. So, I'm going to say, 'nil', to the object. And for the name, if you hit 'Enter' over this, 'NSNotification.Name', it will give you an option to write any name you want. So, I'm going to say something like nil data in here and I'm going to observe a message saying, 'nil' data in the ViewController. And if I see that message, I'm going to call an action. So, it's that simple. We're sending a message here. And if I see this message anywhere in the app, then I can trigger some function, trigger some action. And we already have our action as getData in the ViewController, right? So, if you say, 'NSNotification.Name', you will see something called 'rawValue'. And, you don't have to do '.Name' and open parentheses and you will just see the rawValue in here. So, 'NSNotification.Name("newData"). Now, this will just send the message to the whole app saying newData, okay? And if you don't observe this message anywhere, it won't cause anything, okay? Nothing will happen. But, if you come over here and observe this message, then you can do something with it. But there is a problem right now. We cannot do that in the viewDidLoad, because why? ViewDidLoad only gets called once. So, I'm going to go for another function, which is 'viewWillAppear'. Remember, viewWillAppear gets called whenever we see this ViewController. So, I'm not going to do this under viewDidLoad, but I'm going to do this under viewWillAppear. So, that's the beauty of learning about ViewController lifecycle. So, you're going to say, '.addObserver', okay? And this will add an observer for us and you're looking for the options: observer, selector, and the name. Because the observer will be the ViewController itself because we're going to have a selector again in here. And for the name, we're going to use the same name. So, let me write the selector first and we can just use the getData, right? We already have this function to be called. Now, let me add object to see in here so that it won't display any problem. And in here, you have to say, 'NSNotification.Name('. And for 'rawValue', I'm just going to go for 'newData' and 'object: nil'. So, this will get the message, capture the message that is sent by the DetailsVC. And whenever it gets this message, it will call the '#selector(getData)' and this function will get executed so that we will get this data again. So far, we have three values over here and just go for a fourth one. And add this waterfalls one more time. We can just call this something like 'Nature' and 'Da Vinci' again, and '1500'. As you can see, it got added to my list but it has some duplicates. And, this is because of the previous values in the arrays. We have to clear those values if you want to add these values one more time. So, in the getData function, I'm just going to go for 'nameArray.removalAll'. So, you don't have to keep this capacity, okay? 'idArray.removeAll(keepingCapacity: false)'. And this will solve my problem. So, here we go. Let's test this again and see. Make sure that everything works fine because we are almost completing this app. We have to make sure that everything is working smoothly. So, let's go for another here. I believe this is the same picture. So, I'm going to just go for 'Test', 'Test', and '1500'. And just 'Save' this and here we go. We have the Test here. Of course, we cannot click on them right now because we haven't implemented that feature yet. But, it works, right? We can add new values. So, right now, we have to make a way to go to the second ViewController, again, to display the details of the selected painting. So, that's what we're going to do within the next lecture.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.