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. 

woensdag 23 maart 2016

Closures

In Swift you can see functions more or less as variables. However, the variables in this case do not contain a value. In stead it contains a block with 1 or more tasks. Functions do not have a datatype like ‘normal’ variables but a function type. A closure is actually a function without a name.

Why should we use closures? They seem redundant. Everything you can do with closures you can also do with functions.

As it turns out closures can be passed as arguments to functions and closures can also be returned as functions. You can pass a block of tasks to a function or you can let a function return a block of tasks. Also the code is more compact dan a function. The lesser the code the less errors you can make.

Example:
var dutchOldReadingLessons = [ "aap", "noot", "mies", “wim", "zus", "jet", "teun", "vuur", "gijs", "lam", "kees", "bok", "weide", "does", "hok", "duif", "schapen"]

print(dutchOldReadingLessons)

dutchOldReadingLessons = dutchOldReadingLessons.sort(
  {
    print("\($0) or \($1): ")
    if $0 < $1 {print("\($0) comes first")
      return true
    }
    else {
      print("\($1) comes first")
      return false}
})


























In this Playground an array method is used: sort(). This method returns a sorted array and expects an argument: a closure.

The sort() method is told, via a closure, to how it must sort. The method sort() walks through the array and calls the closure with 2 elements of the array $0 < $1. The closure has to tell if the first element (true or false) has to be placed before or after the second element. By the way, if you look in the console and look what the closure prints for the 2 elements in the array, it does not have to necessarily start with ‘aap or noot:’. Swift decides by it self which 2 elements it takes to compare. If the closure has calculated that the first element has to appear before the second one it returns true. If the first has to go after the the second the closure returns false.

Sort() is responsible for sorting the array but it needs help. In the closure it should be clear so that sort() knows which of the 2 elements in the array comes before the other.

The sort declaration looks like this:

func sort(isOrderedBefore: (T, T) -> Bool) -> [T]

The part between parenthesis is wat sort expects. The argument ‘isOrderedBefore’ is of the function type (T, T) -> Bool. From ‘-> Bool’ you can read that the closure wil return a Bool (true or false) after the compare operation.

From the last ‘->’ you can see that the argument should be a closure that returns an array.
Between parenthesis you see 2 T’s. This means that the closure expects two arguments. T means that it can be any data type. It can be String, Int or Double. (T, T) means that the closure expects 2 arguments of the same type.
The return value [T] means that the datatype wil be the same as the one that was given as argument.

In above example sort() was used. But instead of sort there are also other methods that can be used:
filter()
reduce()
map()

A Closure can also return a function.
The following function is returning another function as its result which can be later assigned to a variable and called.

func jediTrainer () -> ((String, Int) -> String) {
  func train(name: String, times: Int) -> (String) {
    return "\(name) has been trained in the Force \(times) times"
  }
  return train
}
let train = jediTrainer()
train("Obi Wan", 3) 







Bron: Apps bouwen met Swift Versie 2.16 (januari 2016), Roelf Sluman.

donderdag 4 februari 2016

Flappy game


Per mail onderstaande agenda gekregen voor de eerste dag. Mail is in het Engels want er zijn ook buitenlandse studenten in de klas. In totaal zijn we met z'n achten.

-----

Prepare yourself by doing this exercise: https://studio.code.org/flappy/1. The test is only to get an idea of your start qualifications.

After the test you’ll have to wait on the other students to finish. We’ll have coffee and tea for you in the meantime. 

11.00 Introduction + setup MacBooks

Please download Xcode 7.2.1 before Monday (not the Beta version!). Otherwise we have a problem with our broadband: 


13.00 Lunch 

14.00 Social event

17.00 Drinks at Bierfabriek

19.00 Dinner at Bierfabriek

-----

Maar meteen aan de slag gegaan om een beetje te oefenen op code.org. Heerlijk zitten klooien. Dit is het resultaat

Je mag het spelen. Als je de 10 haalt bak ik een taart voor je (graag bewijs meesturen ;-). Voorwaarde is dat ik het in mei bak, eerder heb ik waarschijnlijk geen tijd.

Wens me succes, de eerste dag ziet er iedergeval goed uit!

Beers & burgers, het ultieme nerd-food.






zondag 31 januari 2016

Appsterdam here I come!

Nog een weekje en dan ga ik aan de slag. 12 weken lang elke werkdag van 9 tot 6 ben ik vlak bij het Scheepvaartmuseum in een klaslokaal te vinden. Samen met 6 tot 8 mensen ben ik in de leer bij the App Academy. Hoe maak ik apps voor iOS en alle facetten die daarbij horen komen aan bod.

Korte geschiedenis
Heb je ook wel eens het gevoel 'wat moet ik nu?' Waar ga ik mij in de komende tijd mee bezighouden? Dat had ik vorig jaar (2015) tijdens de zomervakantie, wat een mooi moment was om eens m'n gedachten te laten gaan over dit vraagstuk.

Vlak voor de vakantie had ik met een coach een afspraak gemaakt. Eerste kennismaking gehad. Na de vakantie zouden we een eerste sessie plannen. Voor mijn vakantie had hij een leestip gegeven: 'Ik (k)en mijn ikken'. Dit boek heb ik met veel interesse gelezen; het was een voorbereiding op de sessies na mijn vakantie.

Tijdens de vakantie kwam al pratend met Colette eigenlijk al een heel belangrijk element naar voren waar ik plezier in heb: ik wil graag iets maken. Ik heb veel interesse in techniek, online, nieuwe gadgets, ben ook wel een Apple geek. En dan vergeet ik bijna nog: koken! Ja, als hobby dan, en dat moet het ook blijven. Nee, de vraag was ook waar ik mijn ei in kwijt kan. Met mijn coach had ik nog een uitvoerig gesprek over waar ik warm van word. En daarbij kwam dat er aan het het eind van 2014 door Apple een nieuwe programeertaal geïntroduceerd werd. In artikelen las ik dat het voor newbe's zoals ik een opstap zou kunnen zijn om apps te leren maken. De optelsom hiervoor was eigenlijk 1 + 1 = 3. Mijn hart begon er sneller van te kloppen.

Vragen stellen
Maar, waar ga je dat leren? Hoe ga je dat aanpakken? Waar moet je zijn? Wanneer en hoe lang? Online of in een klaslokaal? Hoe is het eigenlijk om app developer te zijn? Kan ik dat? Wat zijn de toekomstperspectieven? Allemaal vragen... Met mijn coach heb ik een plan gemaakt om ze te beantwoorden. Dat is bij elkaar een halfjaar vragen stellen geworden. Mensen spreken, artikelen lezen, er zelf over nadenken. Mijn gevoel werd steeds sterker dat ik deze weg wilde inslaan. Met mijn leidinggevende bij KLM over gehad. Als ik erover sprak zag ze lichtjes in mijn ogen.

Ik realiseer me dat het een overstap is. Het is weer een verdieping en weer iets kompleet nieuws. In het verleden heb ik ook een overstap gemaakt. Maar nu is het nog meer de diepte in. Met als doel professioneel apps te gaan ontwikkelen. Bij wie, voor wat en waar weet ik nog niet. Aan de andere kant neem ik heel veel ervaring mee. Agile werken, meehelpen aan de realisatie van content in applicaties voor de website van KLM.com.

Knoop doorgehakt
In overleg met mijn leidinggevende nam ik eind november de beslissing om de opleiding te gaan doen. Voor de duidelijkheid: deze opleiding doe ik op eigen titel. Ik heb 2 maanden onbetaald verlof genomen en 1 maand opgespaarde vakantiedagen ingezet. Tja, mijn collega's moeten me minimaal 3 maanden missen. En er is vervanging geregeld. Natuurlijk ook wel een ingreep. Tof dat dat allemaal geregeld kon worden.

Nu is het dan zover. Onverhoopt 2 in plaats van 1 week vakantie opgenomen. Helaas was niet alles in het klaslokaal klaar om al aan de slag te gaan.

Ik beloof om regelmatig een update op mijn blog te posten en zo jullie verslag te doen van mijn reis door app-land. Reacties en vragen zijn altijd welkom.

Ik dank hierbij Colette, de familie, vrienden en niet in de laatste plaats mijn collega's voor steun die ik heb gekregen. Op naar de eerste week. Heb er zin in.