I don’t know if you ever read the book rework, from the founders of basecamp, that’s what I’ve been doing: rework.
In the last posts I’ve been writing about the switch to programming in swift, and the setups for synchronizing data between macOS and iOS through CloudKit. Well, it’s still not finished, and I’ve started reworking my apps. Let me explain.
I’ve rewritten the iOS app in swift, and written a macOS version of the app. So far so good. This is all working fine on the local devices. I can also get data from CloudKit and sent data to CloudKit on both platforms, so that’s fine too. But covering all scenario’s to keep everything in synch, is more challenging than I thought.
You expect to be able to work with all your devices in an off-line mode. You may be on an air plain with your MacBook, or your iPad has no mobile connection, only WiFi… you expect that you can get all data magically synchronized and up-to-date once you get a connection. Your iPad may have not been on for several days, but you updated a lot of stuff on your iPhone…
Deciding how to manage changes you make in your local app, and tracking if the changes are uploaded successfully to CloudKit at first sight seems straightforward. But than you have to managed conflicting changes made on the same record (data) on different devices. So you start tracking changes and the moment that change was made, and compare it to what is in CloudKit… Before you know it you add state and date/time tracking to every field in your database… It’s a mess.
So you rethink what information is stored when, where, how, and what the logic is behind it. You write this out and think of all the possible use cases, to see what works for your app. You try to keep a good balance between complexity and functionality, keeping in mind that for the user, the behavior is normal and consistent… So that’s the challenge for the next weeks or so… reworking change logging and business logic…
Sounds easy enough. I’ll let you know.
About a month ago, I started working on converting an iOS app to Swift 3.0. The iOS app was basically converted a few weeks ago, and additional functionality has been added. The macOS version didn’t exist, so I started from scratch. One of the most important reasons I’m doing this exercise, is to test the CloudKit synchronization over different device and platforms. Not just as a proof-of-concept, but as a completely functioning solution. I already got the synchronization working between my iPhone and iPad, but with the Mac it didn’t work.
Years and years of developer info on stackflow often helps, but for Swift, you better specify that you want swift 3 examples and even than you will mostly get iOS code. With Objective-C you mostly get iOS code as well, but in the case of Swift 3, this is double so.
So you download examples of Apple and others; you look at wwdc videos to see what you missed…. nothing. This should work!
So what I usually do, is continue with other stuff, and in most cases you find the solution sooner of later.
By chance, I was working on another iCloud issue, when I noticed I had switched iCloud accounts. Meaning, that when I’m developing in xCode, I connect with my developer account to iCloud (and CloudKit), and my iMac was using my personal iCloud account… of course this doesn’t work. Accounts are synchronized, not devices, so when you are not logged in to the correct account, you don’t get any updates. OMG, how stupid.
That being said, I do find Apple should allow me to have more than one iCloud account active. Maybe you have a personal and business iCloud account, and you want the corresponding apps to sync, or even get the info of the two account both displayed in the app.
You could share items with other accounts, but this doesn’t really cover it for me.
Anyway, I learned a lot, but basically lost a lot of time trying to solve a problem that didn’t exist.
Have a nice week…
In my last blog I talked about switching our development completely to Swift. So how does it go.
To be honest, better than I thought. A good book and documentation helps of course, but it’s easier and more transparent than I thought. As explained in a previously, I’m rewriting an an existing app, which makes things a lot easier. You don’t need to think about the business logic, so you can focus on the code. In this app, you encounter all the typical code, you’d use in other apps. The if…else, for..loop, Core Data fetching, error handling… etc… You quickly get used to the different syntax, and fortunately the syntax is very consistent and logical.
I do write the code in an Objective-C like way. One of the requirements of Objective-C is that you need to explicitly specify things. A NSString is an NSString and not an Array or a NSMutableString. In Swift you don’t need to do this, but I do specify it in most cases, just because I find it easier to understand. An other way to do this is giving a variable a name that corresponds to the content and it’s type. For example dateString or dateArray… anyways, things are going fine.
Next up is building a web app. The iOS and macOS apps both sync data to iCloud using CloudKit. So now it’s time to have a full functional web app, that more or less matches the iOS and macOS functionality. For this we’ll use CloudKit JS and build a web app with data binding and responsive design. This will take some time, because we haven’t got a lot of experience in this domain. The world of web development keeps on changing very quickly. Frameworks that where popular last year didn’t always keep up and are dropped by developers. This is the a world where you can’t predict what will come next, in contrast to platforms from Apple or Microsoft.
So the challenge is on. I’ll give an update in a few weeks…
Have a nice weekend!
From now on, we’ll always be using Swift as our programming language.
Learning Xcode (Apple’s development environment) and Apple’s programming languages can be hard. When I first switched to Xcode, it took me a while to get used to all the tools, the debugging …etc. Don’t get me wrong, I love working in Xcode, and it has developed nicely the last years, with some really cool features 😉
When I first started working in Xcode, the programming languages to use were Objective-C and C, and I have to say that once you’re used to Objective-C it is a very powerful language. Than Apple introduced Swift as a new programming language in June 2014. Swift 1, was definitely too early (for me) to start with. Swift 2 and 3 followed in 2015 an 2016, and with Swift 3 I got the feeling that it was time to switch. But you are working on this project, and it is so much easier not to learn a new language while you are developing a new App… On the other hand, you don’t want to get stuck with old code, that you’ll have to rewrite in a few years… so when do you switch?
Apple does completely support new functionality in Swift and Objective-C. But what triggered me to switch is the online programming communities, like stackoverflow … etc. Over the last year, it has become clear that most online examples are only in Swift. In general it isn’t difficult to find the matching code in Objective-C, but it is a big sign saying “switch now”. This is of course more so for new features than for older topics.
So I’m rewriting an App from scratch in Swift. Admittedly, it’s not the biggest App, and there’s some redesign on the app… but this way the coding can be a mix or “copying” existing code and adding new code.
New apps that are under development will also be refactored to be in Swift, so that all updated and new Apps will be in Swift.
So this is goodbye to Objective-C and hello to Swift. Go…
Using the Cloud, iCloud for Apple users, is not exactly new. “The Cloud” has been around for many many years now. As a developer you decide how you use the cloud in function of the application you are building.
A social media app will most likely be completely cloud based. A large database app with your personal information will not be cloud based and maybe not even have any data in the cloud, except for backups or something…
Apple provides possible scenario’s to handle all these situations.
* Core Data for your local database storage.
* Core Data for iCloud to sync the database over your devices.
* iCloud Drive and Files in iOS 11 to storage files in the Cloud.
* Cloud Kit, as a file transfer system between your iCloud storage and a device.
You can also use none Apple solutions because of specific features, or because you need a cross-platform solution.
Unfortunately the provided solutions are almost always just a 80-90 percent fit. The reality is that you want local storage to work fast and offline, but you still want all the data in the Cloud, and available on all your devices, the moment you switch to the device.
This means in Apple lingo that you want Core Data and Cloud Kit to work hand in hand and be synchronized automagically. But Core Data doesn’t map on Cloud Kit. These are two separated worlds. But the developer can of course make it map and match for his application.
This is how this works.
* The only through is the cloud.
* Everybody gets the latest updates from the cloud.
* Everybody updates to the cloud.
* All the data that is relevant is in the cloud.
* All relevant data is transferred to a local storage e.g. Core data.
* On your device you always work in the local storage, which than sends the updates to the cloud in the background.
This means of course that there is a lot of error handling to be done to make this work correctly. I guess that’s our job.
So this week I’ll be working on an iOS, macOS, web integration with Core Data and Cloud Kit. I’ll we focusing on our DoDone app, because the database schema is not too complex. There will be a lot of error handling 😉
I suppose this will be the default implementation methodology for the next years.