Live reload when developing server-side

I've been trying my hand a bit at the server-side swift stuff.

My background is Django and what I'm noticing is that the edit-run cycle is a bit long-ish. Two major issues:

  • Live reload seems an essential pre-condition for rapid development. I know that people have stumbled against this problem on the iOS side of things as well and have patched against it using Xcode/Inject. In any case it would be nice to have a way to do hot reload or a plan to mitigate it to enable more interactive ways of development.

  • To make things worse the default server I'm running sometimes will just no longer be responsive to interrupting which means you can't even quit it anymore… making the entire cycle of edit-build-run even longer. No clue whether that is my fault or not but it's kinda odd.

1 Like

Strong +1. IMO hot reloading would significantly improve the flow of creating iOS/server apps and would decrease the development time.

The biggest pain point for me right now when developing iOS apps is the compilation time. Although much has improved over the years, compilation still takes a significant portion of development time. Not only that but it "steals" focus from the current task. What usually happens (and I am not saying it happens to everyone) is that in those 1-2 minutes the project is compiling I hit the web and lose focus.

One of the main pros of new mobile technologies like Flutter and React Native is hot reloading and it would be great if it would also come to Xcode.

1 Like

For me JS hot reloading comes at a very very big expense as you eschew compile time analysis and protection for runtime mishaps that maybe are properly caught with tests and maybe they lead to nasty submarine bugs in name of convenience. I do understand where you are coming from though...

Something like this isn't new or exclusive to dynamic languages, is it? I believe under certain conditions both C# and Java have offered this for a long time now.

1 Like

Fair point. To tell you the truth I don't know what has to be done to enable hot reloading or is it even possible with Swift only. From doing a little research I gathered that existing methods rely on the dynamism of Objective-C. I am only saying that it would be great from a user perspective.

If it means making things a little more dynamic and limiting Swift static guarantees, then maybe we could introduce a special "hot reload" compilation mode just for rapid development. When going to production the user would still have to compile the code normally and would benefit from the usual static checks done by the compiler. We could have the best of both worlds then.

A tool I have used for rust web-dev, where the same problem arises, is watchexec, which you can get from brew (and probably your Linux distro package manager or from cargo). This will re-run a shell command when files matching a pattern change. This in combination with incremental compilation should make the delay from saving to having the new version running bearable.

That being said, hot reloading sounds like it might be an interesting feature, not only for web servers. I doubt there could be hot reloading that skips any of the compile-time checks Swift provides. Your code still has to get compiled somwhere after all.

How exactly that would work (a special compiler mode/a server process that knows how to reload a dylib/whatever) I can‘t really say, @johnno1962 is probably our resident hot-reloading expert and might know more here, but I could imagine problems on the Swift runtime side as well (what happens to lazy static variables for example).

Ah, a watchman and then a quick recompile/restart would be a very nice stopgap measure especially if the frameworks could preconfigure and bundle it.

I'm not too familiar with the internals but I'd say this would only work if one or more Swift classes compile cleanly. Then replace whatever the compilation result is with whatever the previous result was. Hot reload is always a best-effort kinda thing, so for those edge cases you might need to do a full recompile and that is fine.

@ahti, you could probably use a variation of this code on Linux.

1 Like

I use reflex for this (written in Go). Some languages have hot reloading built in that can maintain state while reloading code, but for languages like Swift that just need to be recompiled and restarted, nothing needs to be specific to Swift. And incremental build times are actually really good for server projects.

Compiling is how we’re allowed to slack off: xkcd: Compiling

A lot of work was done on improving build times for the upcoming releases. Try the binaries posted there, maybe it helps!

Also, I don’t know if those changes have been pushed for 4.1 (which was released today). Maybe they have been, maybe not. You could ask there... maybe some of the upgrades have been released already :slight_smile: