AGSPortalUser nullable properties

517
2
Jump to solution
10-04-2019 05:53 AM
SerhiiKyrylenko
New Contributor III

All the properties of AGSPortalUser are  nullable.

The question: is it even possible in practice to have AGSPortalUser instance with username to be null?

I'm trying to relay on this property as a unique identifier of the user and want to be sure if I can force unwrap it with Swift safely.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Nicholas-Furness
Esri Regular Contributor

If the portal user object has failed to load, you might see null. And if the JSON that Runtime gets back from the service is corrupted somehow, then it's technically possible for the username to be null while the JSON was parsed properly. That's clearly an unexpected and highly unlikely edge-case. I myself would not code expecting that.

But in general, I would approach something like this with a guard let statement and/or a precondition.

Something like:

guard let username = user.username else {
    preconditionFailure("Username unexpectedly nil")
}

This will help you during testing to isolate any issues. You could also handle the case more gracefully than with a preconditionFailure() if need be.

View solution in original post

2 Replies
Nicholas-Furness
Esri Regular Contributor

If the portal user object has failed to load, you might see null. And if the JSON that Runtime gets back from the service is corrupted somehow, then it's technically possible for the username to be null while the JSON was parsed properly. That's clearly an unexpected and highly unlikely edge-case. I myself would not code expecting that.

But in general, I would approach something like this with a guard let statement and/or a precondition.

Something like:

guard let username = user.username else {
    preconditionFailure("Username unexpectedly nil")
}

This will help you during testing to isolate any issues. You could also handle the case more gracefully than with a preconditionFailure() if need be.

SerhiiKyrylenko
New Contributor III

Thank you for answer.

0 Kudos