GWT 中的异步回调 - 为什么是final?
我正在开发 GWT 应用程序作为我的学士论文,对此我还很陌生。我在互联网上研究了异步回调。我想要做的是:我想处理用户的登录并显示不同的数据,如果他们是管理员或普通用户。
我的调用如下所示:
serverCall.isAdmin(new AsyncCallback<Boolean>() {
public void onFailure(Throwable caught) {
//display error
}
public void onSuccess(Boolean admin) {
if (!admin){
//do something
}
else{
//do something else
}
}
});
现在,我看到的代码示例直接处理 //do Something// 部分中的数据。我们与监督我的人讨论了这个问题,我的想法是,我可以在成功时触发一个事件,并在触发该事件时相应地加载页面。这是个好主意吗?或者我应该坚持在内部函数中加载所有内容?让我对异步回调感到困惑的是,我只能在 onSuccess 函数中使用最终变量,所以我宁愿不处理其中的事情 - 洞察力将不胜感激。
谢谢!
I am developing an application in GWT as my Bachelor's Thesis and I am fairly new to this. I have researched asynchronous callbacks on the internet. What I want to do is this: I want to handle the login of a user and display different data if they are an admin or a plain user.
My call looks like this:
serverCall.isAdmin(new AsyncCallback<Boolean>() {
public void onFailure(Throwable caught) {
//display error
}
public void onSuccess(Boolean admin) {
if (!admin){
//do something
}
else{
//do something else
}
}
});
Now, the code examples I have seen handle the data in the //do something// part directly. We discussed this with the person who is supervising me and I had the idea that I could fire an event upon success and when this event is fired load the page accordingly. Is this a good idea? Or should I stick with loading everything in the inner function? What confuses me about async callbacks is the fact that I can only use final variables inside the onSuccess function so I would rather not handle things in there - insight would be appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于内部类/匿名函数是在运行时生成的,因此它需要对其访问的变量进行静态内存引用。将 Final 放入变量会使其内存地址静态,将其放入安全内存区域。如果引用类字段,也会发生同样的情况。
Since the inner-class/ anonymous function it is generated at runtime it needs a static memory reference to the variables it accesses. Putting final to a variable makes its memory address static, putting it to a safe memory region. The same happens if you reference a class field.
这只是标准的 java 为什么你只能在内部类中使用 Final 变量。 这里有一个关于这个主题的精彩讨论。
当我使用 AsyncCallback 时,我完全按照您的建议进行操作,我通过 GWT 的 EventBus 触发一个事件。这允许我的应用程序的几个不同部分在用户登录时做出响应。
Its just standard java why you can only use Final variables inside an inner-class. Here is a great discussion discussing this topic.
When I use the AsyncCallback I do exactly what you suggested, I fire an event though GWT's EventBus. This allows several different parts of my application to respond when a user does log in.