[英]iOS swift 4 sets a default boolean to complicated static variables?
Hi I'm working on an app that is connected to a Web API and contains a local Database (Realm). 嗨,我正在开发一个连接到Web API并包含本地数据库(Realm)的应用程序。 I have a User class which is a Realm Object
subclass and keeps the user information such as username
, password
, accessToken
and etc. It has also a static variable and computed property called instance: User?
我有一个User类,它是Realm Object
子类,并保留用户信息,如username
, password
, accessToken
等。它还有一个静态变量和称为instance: User?
计算属性 instance: User?
and current: User?
current: User?
Please find details below 请在下面找到详细信息
class User: Object {
/// Properties (Database Columns)
@objc dynamic var username: String = ""
@objc dynamic var password: String = ""
@objc dynamic var accessToken: String = ""
/// Current Logged In User
static var current: User? {
if instance == nil {
let realm = try! Realm()
instance = realm.objects(self).first
}
return instance
}
private static var instance: User?
}
As you can see current
property returns the first User
instance from realm database which is the logged in user information if there is any, otherwise it returns nil
which means the user hasn't logged in the app. 如您所见, current
属性从领域数据库返回第一个User
实例,如果有则返回已登录的用户信息,否则返回nil
,这意味着用户尚未登录应用程序。
I have also another class called Configs its name can describe what it does, there's a static boolean variable in it called isLogin
. 我还有另一个名为Configs的类,它的名称可以描述其功能,其中有一个名为isLogin
的静态布尔变量。 Here is the part of my code which I defined this 这是我定义的代码部分
static var isLogin: Bool = User.current != nil
as you can see this property refers to User.current
property described above and returns the result. 如您所见,此属性引用上述的User.current
属性并返回结果。 I call Configs.isLogin
several times in the app whenever I want to check if the user is logged in or not so, I can tell the app to behave properly. 每当我想检查用户是否登录时,我都会在应用程序中多次调用Configs.isLogin
,我可以告诉应用程序正常运行。 But when I call this property, it seems swift dive into the property and calls User.current
JUST ONCE in an app session, the next times it just keeps the first result and assign it directly to Configs.isLogin
and it will no more check the User.current
property and won't runs query to see if there is any result or not, thus when for example the user fills and submits the registration form and gets a token from API and I save the information in Realm, if I call Configs.isLogin
it still returns false since the first time in this session that it checked the User.current
it was nil, but if I call User.current
directly (after the registration) it's not nil anymore and I can access to the logged in user, also if I relaunch the app and start a new app session Configs.isLogin
returns true which is correct and shows swift dives into the User.current
and checked it and returned the proper result (as I mentioned at first of the article, only the first time it will dive into the class and checks th 但是,当我调用此属性时,似乎迅速进入该属性并在应用程序会话中调用User.current
ONCE ,下一次它仅保留第一个结果并将其直接分配给Configs.isLogin
,它将不再检查User.current
属性,不会运行查询以查看是否有任何结果,因此,例如,当用户填写并提交注册表格并从API获取令牌时,如果我调用Configs.isLogin
,则将信息保存在Realm中Configs.isLogin
,因为自从在该会话中第一次检查User.current
以来,它仍然返回false,但是如果我直接(在注册后)直接调用User.current
,则它不再为nil,并且我可以访问登录的用户,同样,如果我重新启动该应用程序并启动一个新的应用程序会话,则Configs.isLogin
返回true,这是正确的,并显示了对User.current
快速潜水,并对其进行了检查并返回了正确的结果(如我在本文User.current
提到的那样,第一次它将进入课堂并检查 e result). e结果)。
I debugged my app several times and several parts until I found this problem, How can I make swift to always dive into User.current
from Config.isLogin
property when I call this config? 我对应用程序进行了多次调试,直到发现此问题为止。如何在我调用此配置时迅速从Config.isLogin
属性中跳入User.current
? is there something wrong with my code? 我的代码有问题吗? and if yes what's the proper way? 如果是,正确的方法是什么? Thank You 谢谢
Currently isLogin
is a variable initialized at first access. 当前, isLogin
是在首次访问时初始化的变量。 You can move the initialization part into a computed property so it always checks on User.current: 您可以将初始化部分移动到计算属性中,以便始终检查User.current:
static var isLogin: Bool { return User.current != nil }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.