- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我希望我能以适当的方式解释问题:)我有一个对象数组 (handleData)。我从数据库中获取它们。我想通过为每个单独调用服务将它们发送到服务器。我将服务放在一个 for 循环中以发送所有 handleData(请参阅代码)。
调用服务是在后台完成的。每个人的回应可能不会来,因为他们有条不紊地发送。我必须为我发送的每个 handleData 做一些更新。
问题:当响应到来时,我不确定是否对我想要/正确发送的确切 handleData 执行了所考虑的操作(记录更新)。
private void sendDataOfTemplates() {
ArrayList<FormHandleData> formHandleDatas = FormHandleData.getDatasFromDB(getContext(), 12, EnumDataStatusOfServer.NoSTATUS.getIntValue(),-1);// true means >> to send / -1 means no limit
try {
if (formHandleDatas != null && formHandleDatas.size() != 0) {
for (int i = 0; i < formHandleDatas.size(); i++) {
final FormHandleData handleData = formHandleDatas.get(i);
if (handleData.status_in_server == EnumDataStatusOfServer.OPEN.getIntValue())
if (handleData.status_in_app == EnumDataStatusInApp.SAVED.getIntValue() || handleData.status_in_app == EnumDataStatusInApp.EDITED.getIntValue()) {
ServiceHelper.getInstance().sendDataOfTemplates(new ServiceHelper.ResponseListener() {
@Override
public void onResponse(String response) {
try {
SimpleResponse simple_response = new Gson().fromJson(response, SimpleResponse.class);
if (simple_response.isSuccessful()) {
handleData.status_in_app = EnumDataStatusInApp.SENT.getIntValue();
FormHandleData.UpdateDataTemplatesInDB(handleData, getContext(),false);
} else {
}
} catch (Exception e) {
}
}
@Override
public void onErrorResponse(VolleyError error) {
}
}, handleData);
}
}
}
} catch (Exception e) {
}
}
最佳答案
problem : when the response comes I am not sure that if the regarded action (update of record) is done to the exact handleData that I want/sent properly.
如果我理解正确的话,你是在问你的匿名 ServiceHelper.ResponseListener
子类正在访问的周围局部变量 handleData
是否始终是同一个对象实例,即使尽管在下一个 for
循环中,该变量的值将有所不同。答案是肯定的。所以您不必担心。
如果您想了解更多关于匿名类如何从周围范围捕获变量的信息,请阅读 this part of the Oracle's Java tutorial ,它说:
- An anonymous class has access to the members of its enclosing class.
- An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.
因此,可以访问周围变量的事实意味着从匿名类的角度来看它(有效地)是最终的,即它不会改变。
这里是一个使用多线程的小演示:
package de.scrum_master.app;
public class WhoDoesWhat {
private String name;
private final String action;
public WhoDoesWhat(String name, String action) {
this.name = name;
this.action = action;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name + " -> " + action;
}
}
package de.scrum_master.app;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Application {
private static final Random RANDOM = new Random();
public static void main(String[] args) {
List<WhoDoesWhat> peopleDoingSomething = new ArrayList<>();
peopleDoingSomething.add(new WhoDoesWhat("Galileo", "discover moons of Jupiter"));
peopleDoingSomething.add(new WhoDoesWhat("Johannes", "determine the laws of planetary motion"));
peopleDoingSomething.add(new WhoDoesWhat("Albert", "explain the precession of Mercury"));
peopleDoingSomething.add(new WhoDoesWhat("Edwin", "notice something odd about recession speeds of galaxies"));
for (WhoDoesWhat personDoingSomething : peopleDoingSomething) {
new Thread(() -> {
System.out.println("START " + personDoingSomething);
try {
int waitCycles = 1 + RANDOM.nextInt(10);
for (int cycle = 0; cycle < waitCycles; cycle++) {
System.out.println(" " + personDoingSomething.getName() + " is still being busy");
Thread.sleep(250);
}
} catch (InterruptedException e) {
}
System.out.println("STOP " + personDoingSomething);
}).start();
}
}
}
控制台日志可能如下所示:
START Johannes -> determine the laws of planetary motion
START Albert -> explain the precession of Mercury
START Galileo -> discover moons of Jupiter
START Edwin -> notice something odd about recession speeds of galaxies
Albert is still being busy
Johannes is still being busy
Edwin is still being busy
Galileo is still being busy
Galileo is still being busy
Edwin is still being busy
Johannes is still being busy
Albert is still being busy
Edwin is still being busy
Galileo is still being busy
Albert is still being busy
STOP Johannes -> determine the laws of planetary motion
Edwin is still being busy
Galileo is still being busy
Albert is still being busy
Galileo is still being busy
STOP Edwin -> notice something odd about recession speeds of galaxies
STOP Albert -> explain the precession of Mercury
Galileo is still being busy
Galileo is still being busy
Galileo is still being busy
STOP Galileo -> discover moons of Jupiter
关于java - java中for循环内部的后台服务调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47032499/
从 0 开始搭建一套后台管理系统,成本巨大,所以都会选择一套成熟的组件库,基于此,再堆叠业务逻辑。我们公司的组件库基于 Ant Design。Ant Design 包含一套完整的后台解决方案,不仅
在我的 IOS 应用程序中,我有一个标记为 retain 的 NSDate* 属性 当我的应用程序再次激活时,属性值已被释放。 我是否误解了属性和内存管理的工作原理,我该如何防范? 最佳答案 很明显,
我有一个使用 BackgroundWorker 组件的示例 WinForms 应用程序。它工作正常,但是当我点击 Cancel 按钮取消后台线程时,它并没有取消线程。当我点击 Cancel 按钮调用
我目前正在开发一个应用程序,该应用程序在启动时会对服务器执行 ping 操作,该服务器会为每个连接的设备返回一个唯一标识符。设备每 5 秒从服务器检索另一页以获取一组不同的数据。这个唯一的 ID 可以
我正在开发一个应用程序,当它通过主页按钮在后台按下时,计时器应该启动,当应用程序返回前台并且计时器已经过了一定时间时,应该是执行。 我的问题是 当我的应用程序转到背景/前景? 是否有特殊的方法或其他技
我有 map View ,其中几乎没有 MKPointAnnotation。 一切正常,但是, View 的 MKPoiintAnnotation 的“背景”是“不可见的”,因此不是很“可见”。 我想
我在 iOS 中开发广告数据应用程序。我的应用程序广告数据在前台很好。但我想在 ios 后台宣传信标数据。我设置了背景外设设置。和广告数据 advertisingData = [CBAdvertise
如果我有一组操作,我想根据特定条件在后台工作程序中运行,例如,我有 10 个条件 if(a) BackgroundWorker doA = new backgroundworker() if(
我想独立运行一个函数。从我调用的函数中,我想在不等待其他函数结束的情况下返回。 我试过用 threadind,但这会等待,结束。 thread = threading.Thread(target=my
我想在用户在线时立即执行一些任务,即使他在后台也是如此。我正在使用 Reachability 类来检查互联网。但是当我在后台时,这个类没有通知我。我知道有人早些时候问过这个问题,但没有找到任何解决方案
我在后台播放文本转语音时出现间歇性(哎呀!)问题,由 Apple Watch 触发。我已经正确设置了后台模式、AVSession 类别和 WatchKitExtensionRequest 处理程序。
我有一个相当复杂的程序,所以我不会在这里转储整个程序。这是一个简化版本: class Report { private BackgroundWorker worker; public
我有一个任务在 backgroundworker 中运行。单击开始按钮,用户将启动该过程,并获得一个取消按钮来取消处理。 当用户点击取消时,我想显示一个消息框“进程尚未完成,你想继续吗”。 这里我希望
我有一个按以下方式编码的脚本。我想将它作为后台/守护进程运行,但是一旦我启动脚本,如果我关闭它从程序运行的终端窗口终止。我需要做什么来保持程序运行 loop do pid = fork do
我正在制作一个使用 ActivityRecognition API 在后台跟踪用户 Activity 的应用,如果用户在指定时间段(例如 1 小时)内停留在同一个地方,系统就会推送通知告诉用户去散步.
当尝试使用 URLSession 的 dataTaskPublisher 方法发送后台请求时: URLSession(configuration: URLSessionConfiguration.ba
当我编译这段代码时,我得到了他的错误,对象引用设置为null,错误位置在Dowork中,argumenttest.valueone = 8; public partial class Form1 :
有什么方法可以使用最小化或不活动的应用程序吗?我可以打开我的应用程序,然后打开并使用另一个应用程序,然后按一个按钮来激活我的程序吗? 例如,打开我的应用程序,打开 Safari,按下按钮(F1 或任何
我的具体要求是一个在后台运行的应用程序,被通知显示器即将进入休眠状态或者设备已经或即将达到空闲超时 - 然后唤醒并执行一些(简短的)一段代码。 我在这里找到了有关应用程序被置于后台或暂停的通知的引用:
我有一个 LSUIElement 设置为 1 的应用程序。它有一个内置编辑器,因此我希望该应用程序在编辑器打开时出现在 Cmd+Tab 循环中。 -(void)stepIntoForegrou
我是一名优秀的程序员,十分优秀!