dinsdag 12 april 2016

Non-relational database using relations


Until now I knew about the existence of databases, that they hold information in a structured way and that they are used in nearly any mobile app or more sophisticated web application. Also I heard about the difference in databases. You have the old SQL version which holds tables and the NoSQL database and many varieties in between. But how you create, make, maintain one and use it in an app is a totally different story. The title holds a contradictio interminus. In this article I will describe how to use relations between data in a non-relational database works.

The app I’m working on should make use of the following information. Companies which will hold an address, telephone number and the people who work for the company. Furthermore there is an ability to add an event on a public event calendar. An event can be added by somebody who works for one of the companies which are also part of the database. In the event calendar the persons who want to attend an event can indicate in the app by tapping the attend button.

For simplicity sake I leave the amount of data as it is. What we can learn from above is that in the database there is need for three nodes. A company node, a person node and an event node. Each of these nodes have children. In every child specific information is stored. For instance: in a child of the company node the address is stored and in another child their telephone number. What about their coworkers? Aha, here we come across a new concept. Normally, you would think that a coworker is a member of the company and therefore one of the children in the company node. Wrong, that is where the Person node comes in handy. As there is a one to many relationship between a company and a coworker we can make a reference in the Person node with a child which refers to the company this person is working for. In this example we assume that a person can only work in one company for simplicity sake.

An the event functionality is another relationship as the one between companies and people. People can make multiple events and events are attended by multiple people (hopefully). This is what we call a many to many relationship. How do you make it possible in the database has this kind of relationship? The simplest way is to make a new node. In this case there should be a node that holds a relationship between the events and the people who attend it or a person who has made multiple events. A new node should be called something like PersonEvent to indicate the relationship. In the node a reference should be made to the event on the one hand and the person on the other hand.

The database we are using for our project sends a JSON message. The database does not present filtered info after the app has queried it. This means that the app should filter out what has to be displayed. In the app code should help to do this filtering.

Below tree represents the data. The colored boxes indicate the relationship of the data.


maandag 4 april 2016

The use of the companies class in the Marineterrein app.

My fellow student and I are building an app for the Marineterrein. Many companies are established on a big area that is called Marineterrein and is situated in Amsterdam. To get more collaboration between the companies on the Marineterrein the idea was raised to create an app. Part of the app is the ability to search in a register of companies on the Marineterrein. 

In the register data will be stored like, name of the company, telephone, address, location, logo and telephone number. And some other info as well. These are known as properties in Swift. In the app a separate file is created. The file is called Companies.swift. In the file a class is created. The class is called Companies. It is a class in which the data is initiated which should be available in the app. 

As said the class Companies has properties. A class can have stored properties, which can either be constants or variables. One of the main features of classes is inheritance: a class can inherit methods, properties, and other characteristics from another class. When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its superclass. Also a class can be used as a blueprint of an object: a drawing of a building for instance. Classes are also reference types. They have their own unique identity.

The first part of the class of the app looks like this:

class Companies: NSObject {
    var nameCompany: String
    var address: String
    var phoneCompany: Int
    var emailCompany: String
    var imageCompany: UIImage?
    var url: NSURL
    var entranceCode: String

The class Companies inherits from NSObject. This is a called a root class: it provides basic support for Objective-C runtime. NSObject is the base class that most Objective-C classes inherit from. All of the UIKit classes like UIView, UITextField and UIViewController inherit directly or indirectly from NSObject. NSObject is needed of your class (ie Companies) need to interface with the runtime system. In our case it is the URL of the website of the company that is stored. A URL is of type NSURL in Swift and therefore should the class inherit from NSObject.

For every property it is indicated which data type it should have. String is a range of characters, Int is a whole number, UIImage is a specific data type for images and NSURL is  a specific one for the url variable.

After the declaration of the blueprint all the properties are initiated. In a class the properties have to be initialized. The properties have to have a first value. Below the first part. Here the parameters of the initializer are defined.
init(nameCompany: String, address: String, phoneCompany: Int, emailCompany: String, imageCompany: UIImage, url: NSURL, entranceCode: String) 

After the initialization the properties are set to their real value. 

{
self.nameCompany = nameCompany
      self.address = address
      self.phoneCompany = phoneCompany
      self.emailCompany = emailCompany
      self.imageCompany = imageCompany
      self.url = url
      self.entranceCode = entranceCode
}
}

In above lines it says that the property (self.nameCompany) is the same value as the parameter of the initializer.


Now the class is set up and ready to use. All company records will have the same properties (read details) to make the register complete.