RealmExplorer – a piece of sample code for Realm DB

realmexplorer_01_smallFirst things first, look at the RealmExplorer github repo for the project.

Preface: CoreData

I have some history in using databases. From using Oracle, Frontbase, mySQL, PostgreSQL, sqlite etc. the last time I used a database was for a postcard app using CoreData. But I found CoreData to be a lot of overhead and boilerplate code to write.

Especially migrations are kind of not very intuitive to understand and if something goes wrong you are basically lost. While Xcode for a while had an Entity modelling tool (which had it’s roots in the WebObjects EOModeler), most of the time this tool did not work very well for me and had lots of bugs. In each Xcode version different bugs!

Also many people report running into issues when using CoreData with lots of objects, because it is easy to loose track of memory consumption with CoreData and if you do not actively check and work against that, you end up with a whole model graph being in memory. Not cool! So I wanted to try something different.

Realm

I found Realm by Realm.io to be interesting enough to tinker with. So I started working with their sample code projects. But none of those projects came even close to some real world example. So I just started prototyping something I will need for a different project anyway.

Starting with a custom viewcontroller for adding & editing entities I recognized that this would soon become rather painful to provide e.g. inputfields for text and numbers and dates. So I grabbed FXForms to give it a try after I had already completed a viewcontroller to manage DBCaptain entities a bit.

Migrations

Right from the start I wanted to know how migrations work. Because usually those are the weakest spot in every db operation taking place. With a live product you always „pray“ that nothing will go wrong during migrations on the customers device. And keeping track of changes to the db schema is very important to have a stable app.

So I built the sample around the process to „simulate“ an app in development. You can now iterate through different development steps (5 different app states & their migrations) and their needed migrations. This way I figured out how I can safely manage my migrations (i.e. always keeping a backup of the old schema). In the project the
DatabaseFactory.m class manages all that. It detects existing schemas using a precise naming scheme for each Realm-db created.

During app launch any necessary migrations are executed if necessary. Also, any errors happening while the app launches and gets into trouble activating the database, are nicely displayed.

Create/Insert/Edit/Delete

I started with one entity DBCaptain which is basically a user entity. I just wanted to create user objects and populate them with different properties. I tested insertion with one and with many users on the main thread and on the background thread. I also took advantage of notifications coming from Realm as soon as an operation finished updating the db.

Deletion of objects came next. As always having the UI keeping track of all those changes is the more difficult task. So expect some „ugly“ code, because this was only me tinkering around. When i wanted to edit existing objects I recognized that I do not want to create all those textfields manually.

FXForms

That was when FXForms came to the rescue. I started to recreate the already half done viewcontroller and created another based on FXForms. I needed some time to wrap my head around FXForms, because basically this is a quite sophisticated hack of providing compact descriptions of UITableViewCells and their content.

You describe what kind of structure your form should have and what kind of value types it should use. FXForms crafts the UITableView which makes all those inputfields come true. Things start to become a bit tricky if you want to control those UITableViewCells more directly. E.g. I needed a way to exclude certain properties of being edited at all. So I simply extended the FXForms protocol to allow for a denial of userInteractionEnabled on certain cells by adding the FXFormFieldEditable key. This is used on the CaptainFormViewController to exclude the information about the database schema and the encryption status to not be editable.

Evaluation

My first impression of Realm DB is quite positive. It is fast, it provides helpful error-messages and it is easy to setup and comprehensive in what it does and how it works. I never in my life had migrations up and running that quickly. Very, very helpful was the Mac app RealmBrowser to open a realm DB file directly. I used that while checking my migrations and the results of operations which added arbitrary data (e.g. image files).

What is actually really great is that encryption is supported for the db by just providing a 128 bit key. That is really helpful to protect user data and increase safety of the realm db file even if it gets backed up into some cloud somewhere else.

What’s next?

I think I missed something related to the KVO-thingy going on by Realm. But I had no time until now to figure that out. I suppose they actually track entities and changes via KVO. But I am not yet sure how exactly this works and how I can detect if an object has pending changes. That is why I added a hasPendingChanges BOOL-flag to both ViewControllers for editing entities. So help is welcome here!!

I did not have time to do some real hardcore performance testing, i.e. adding thousands of entities and relationships in a short time and crafting a rather complex model with complex relationships which define ownership and cascading deletions etc. Also doing sophisticated db queries is on my list to do next.

But having a prototype now to tinker with helps already a lot. Since I learn best from sample code, and I think I am not the only one learning new things this way, I hope it helps others to jumpstart with Realm, too.

Why do I blog this? I just wanted to give a bit of background info on the sample code I put on github. Have fun tinkering & I am really in need of getting more info on how I can detect changes to an object so I know WHEN to save changes to the db. Maybe someone who already grabbed the concept better can help me here?? Leave a comment or contact me via github.

WWDC & iOS 9 Expectations

ios_9_expectations_wwdc

What I expect today Monday, 8th June 2015 in one sentence? They will present Apple Music Streaming in a slide at around 0:20 and then keep raving about the endless possibilities for developers and Apple until delta-t = -0:05, when they will mention that they’ll gonna build their own search engine like Google called heureka! to improve user experience & search results for Siri on the Apple Watch…

iOS

  • another all new awesome design for iOS 9 or 10, again(!)
    …after mr. ive found someone else to clean up the wreckage & promoted himself away from the heat of the kitchen
  • iOS 9 (codename: sirene) announcement for autumn 2015
  • swift, swift, swift … even more swift
  • watch, watch, watch … even more watch
  • indie, indie, indie … even more indies. wait… that was 2008.
  • developers, developers, developers … even more developers. wait… that was 2008, too.
  • elimination of the home & touch id button. i.e. removed mechanical movement.
  • iOS multiwindow support, because ya know, things are too easy to use right now and we actually learned how to build „complications“ from the watch…
  • new resolution and/or new aspect-ratio screen/device/rectangle
  • all lightning ports get killed™ by USB-Type-C (was a nice, short time lighting)
  • a new privacy option for GPS: allowed only if moon has declination of 45 degrees or higher & has full moon phase
  • Setch™ the absolutely stunning new search engine to search for settings & options on your iDevice
  • TouchID will gain more options to configure „shortcut-fingers“ to directly open specific apps and/or execute certain tasks, like compose a new tweet.
  • …to be continued.

OS X

  • OS XI with even less control left for the user. more drm. more hw-dongle.
  • new finder (just kiddin‘!)
  • iTunes with beats™ streaming
  • iTunes with a usable & consistent UI (just kiddin‘!)
  • CAPS LOCK gets killed™ from the keyboard. (just kiddin‘!)
  • Xcode Cloud, you will be able to code in the Safari Browser; all your code is autosaved in iCloud Drive; better swift support, too; 5 GB storage space sold separately
  • iDevice Developer Lab Kit, you can order a nicely designed rack for mounting all your bought iDevices for debugging. comes with one USB-Type C port, adapters for lightning to usb and usb-type-c to usb3 sold separately
  • apple pay for the desktop
  • apple tv officially replaced by mac mini running OS XI TV edition™ with kodi
  • wifi will be fixed (just kiddin‘!)
  • you will no longer be able to disable gatekeeper for non-appstore-apps
    you will need to enter your password everytime instead (that is more secure, ya know)
  • a new mighty magic force touch wireless mouse running on wifi instead of bluetooth to enable long distance mouse handling, will be presented, it will be amazing like all the others (and maybe carry TouchID)
  • from now on everything is labeled beta makes things easier for everyone, even though it is already 2015 and not 2008.
  • management found a way to increase the budget to fix software quality by increasing apple cut to a 40% of developer revenue starting 24th of october 2015
  • TouchID will be made available to other devices via a new keyboard (or mouse)
  • …to be continued.

Change

Please compare…

and…

Source: Users don’t hate change. They hate you. — The 9x Effect Applies to Redesigns Too

Post WWDC Recap

Things predicted/expected successfully…

  • Searchable settings for iOS aka Setch™
  • Multiwindow aka splitscreen support
  • swift, swift, swift only code examples
  • beats(one) streaming music (aka internet radio revolution)
  • everything beta™ now is a reality (swift, iOS, OS X, watchOS, Apple TV, Connect, Apple Music)

Others reflecting

Why do I blog this? As usual for the fun of it of guessing the things to come and as a catharsis to clean myself of all the pain I had with iOS 7 transition. Oh and btw, the picture at the top… I chose the Ulysses and the Sirens lithograph by Otto Greiner as a motive (maybe I should have chosen this one, bc. this one guy fettering Ulysses to the boat I do not see him anywhere at Apple…) & the music for several reasons…

  1. The Emperor’s New Clothes is still valid (that’s why people are naked).
  2. the sirenes are still in full swing singing their song to augur the promised land of awesome design
  3. the picture has lots of gfx interruptions in resolution which stand for the overall UXP of iOS & OS X which now is driven by interruptions & glitches all over the place
  4. the song from „Die Antwoord“ (was removed from soundcloud) „Pantera“ expresses without any lyrics perfectly the state of the union: broken

All this makes it hard to believe in my own wishes which I will hold back this time, because they seem to not matter at all any longer.

My realistic take on Apple’s future with a valid business model? They could just throw all money on encryption & Apple Pay to push and create and be the Core Payment Infrastructure of the future™. In 2017 presenting a blockchain based payment extension will cement the future path of Apple as the new VISA/MasterCard-Killer worldwide. They are a bank already… so it’s just one small step for Tim Cook… but…

iTunes: Syncing photos without it.

I don’t get iTunes. I am a huge fan of a TV serie on BBC. I found some great wallpaper graphics on their website and the web. I just wanted to transfer these images to my iDevice. And when I fired up iTunes to do that (because there is no other Apple recommended way of doing so) things went south quickly. See following screen…

iTunes_fail

I selected (for a test) 1 photo and wanted to sync that to the device. See what happened. Out of the blue iTunes killed all my apps, reorganized all apps, reordered all apps icon positions on the device and did NOT sync the photo. Huge success!!

tl;dr – use PhotoTransfer Mac App

phototrans_small

Import and export your whole photo library all at once: Moving thousands of precious photos from a messed library worries everyone. PhotoTrans comes to rescue by featuring an innovative and unique batch photo transfer. Imagine that, through the high-efficient batch mode, you’ll be able to transfer thousands of photos or even your entire library between iPhone, iPad and computer, all at once.

Taking every piece of your photography works into account, PhotoTrans cares every single picture and even the whole photo library on your iOS devices, including the captured snapshots in camera roll, the photo stream bound to iCloud and other pictures imported from iTunes library.

You get it here. it allows to comfortably manage you photos in both directions without the need to use iTunes.

Update 6.8.2015: AnyTrans & PhoneClean

DO NOT BUY ANY OF THOSE PRODUCTS. IT IS SNAKEOIL AND DOES NOT WORK.

Why do I blog this? Because I lost an hour of my lifetime in trying to achive what I wanted and nothing happened other than iTunes creating a mess on my device. So do yourself a favour and use the right tool for the task. Thank me later!