【发布时间】:2017-10-06 19:57:08
【问题描述】:
对于我的应用,我必须运行两个操作,都是异步的:
从文件读取(我使用这个文件来模拟从数据总线读取)-异步操作,因为我不知道“何时”到达新的 总线上的消息/字符。我搜索一个特定的序列 字符,例如帧 start_bytes = "xx" 和随后的 4 个字节是 我等待的“数据”。
根据从文件中读取的“数据”读取/更新数据到 Firebase - 由于使用 addValueEventListener 导致的异步操作。
我正在考虑一种信号量/互斥体机制或一个简单的布尔标志,即一个任务向另一个任务发出信号,表明必须将新数据保存/更新到 Firebase。
如何同步这两个操作(通过将它们嵌入到任务/AsyncTask/线程中)?
我搜索了这些主题,但发现了与 UI、ProgressBars 等相关的示例 .. 对我的情况不太适合/有用。
在 Firebase 中读取/更新数据
myRefDevices.addValueEventListener(new ValueEventListener() {
// addValueEventListener
// This method is called once with the initial value and again
// whenever data at this location is updated.
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
boolean bChildFound = false;
DatabaseReference dbrefChildFound;
final CDeviceStatus obj_new = new CDeviceStatus();
for( DataSnapshot val : dataSnapshot.getChildren() )
{
if( val.getKey().contentEquals(MAC_ADDRESS[ iIterator ]) )
{
bChildFound = true;
dbrefChildFound = val.getRef();
obj_new.setiAvailable_A( val.getValue( CDeviceStatus.class ).getiAvailable_A() + 1 );
obj_new.setsID(val.getValue( CDeviceStatus.class).getsID() );
dbrefChildFound.setValue(obj_new);
}
}
if(!bChildFound)
{
Log.d("child=" + MAC_ADDRESS[ iIterator ], "not found");
}
if(++iIterator == 16)
{
iIterator = 0;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
从文件中读取:
try {
// open input stream text file for reading
Resources res = getResources();
InputStream instream = res.openRawResource( R.raw.simulated_bus );
// we convert it to bufferred input stream
BufferedInputStream bistreamSimulatedBus = new BufferedInputStream(instream);
try{
// if we want to stop reading from the file / simulated bus for whatever reason..
boolean bStayInLoop = true;
while ((bistreamSimulatedBus.available() > 0) && bStayInLoop)
{
try {
// throw new InterruptedException();
char c = (char) bistreamSimulatedBus.read();
if( COUNT_CHARACTERS_NEWLINE )
{
if ( '\n' == c ){
// we can count how much NewLine character we have
//iNL_Counter++;
}
}
...
}
catch ( InterruptedException e ) {
throw new RuntimeException( e );
}
}
} catch (IOException e) {
throw new RuntimeException( e );
}
finally {
// release any resource associated with streams
if ( null != instream ) {
instream.close();
}
if ( null != bistreamSimulatedBus ) {
bistreamSimulatedBus.close();
}
}
}
catch (Exception e) {
throw new RuntimeException( e );
}
谢谢。
【问题讨论】:
标签: android asynchronous firebase android-asynctask firebase-realtime-database