Reachability.swift: Connection.none can cause confusion

Created on 23 Oct 2017  ·  7Comments  ·  Source: ashleymills/Reachability.swift

Hi,

the documentation says "isReachable has been deprecated. Use connection != .none instead".
This can cause confusion when used together with optionals. Here is an example:

Assume you store the reachability instance into an optional property:
var reachability = Reachability()

And then later you want to do something if you are not connected:

if reachability?.connection == .none {
    // show offline message, for example
}

The code inside the if statement will only be called if reachability is nil, not if it has a value and connection equals .none (because Swift checks if the optional value _reachability_ equals Optional.none). To make it work when _reachability_ is an optional property, you have to write

if reachability?.connection == Reachability.Connection.none {
    // show offline message, for example
}

Thats why it might be better to rename Connection.none to something else or mention it in the documentation.

Most helpful comment

Connection.unavailable sounds good to me 👍

All 7 comments

@hamchapman We should look at renaming this I think… I'm struggling to think of good name. Connection.noConnection repeats "connection" so that's out. Maybe .off?

@ashleymills @hamchapman I think Connection.unavailable works best here.

@DanielStormApps Perfect!

Connection.unavailable sounds good to me 👍

Just faced this confusion after updating - it tricked me! 😄
Renaming Connection.unavailable in an upcoming release sounds great 👍

I encountered this issue recently. Are there plans to actually implement this fix?

I just pushed this fix - will be in the next release

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hvsw picture hvsw  ·  4Comments

nadimalam picture nadimalam  ·  7Comments

AlekseiR picture AlekseiR  ·  4Comments

perlguy99 picture perlguy99  ·  4Comments

X901 picture X901  ·  5Comments