简体   繁体   English

当两个地图都仍打开时,如何使用B4A中的两个地图对象更新一个地图文件?

[英]how to update one map file using two Map objects in B4A when both Maps are still open?

how to update one map file using two Map objects in B4A when both Maps are still open? 当两个地图都仍打开时,如何使用B4A中的两个地图对象更新一个地图文件?
My code is : 我的代码是:

dim MapForUser1, MapForUser2 as Map<br>
MapForUser1 = File.ReadMap(File.DirInternal, "scores.dat")
MapForUser2 = File.ReadMap(File.DirInternal, "scores.dat")

One user updates some key value of 113: 一位用户更新了一些关键值113:

Dim s1 as String
s1 = MapForUser1.Get("113")  

here update s1 then delete the key and add new item with same key 在这里更新s1然后删除密钥并使用相同的密钥添加新项目

MapForUser1.Remove("113")  
MapForUser1.Put("113", s1)  

File.WriteMap(File.DirInternal, "scores.dat", MapForUser1)

it works very good in all situations 它在所有情况下都很好用
If second user updates value of same key 113: 如果第二用户更新相同密钥113的值:

Dim s2 as String
s2 = MapForUser2.Get("113")

here update s2 then delete the key and add new item with same key 在这里更新s2然后删除密钥并使用相同的密钥添加新项目

MapForUser2.Remove("113")
MapForUser2.Put("113", s2)
File.WriteMap(File.DirInternal, "scores.dat", MapForUser2)

it works very good in all situations 它在所有情况下都很好用
It shows everything good but second user's update is added as a new Item rather than updating the existing item with key 113 它显示一切正常,但第二个用户的更新被添加为新项,而不是使用键113更新现有项

Both users use same Activity of an App at the same time. 两个用户同时使用同一应用的“活动”。 how to manage the second user's update properly when first has already opened the map for a file. 当第一个用户已经打开文件映射时,如何正确管理第二个用户的更新。 Second user is unable to delete the item for the same key that first user updated first but didn't close the activity. 第二个用户无法删除与第一个用户首先更新但未关闭活动的密钥相同的项目。 If first user closes the activity and then second user updates same item, then it is working. 如果第一个用户关闭活动,然后第二个用户更新同一项目,则该项目正在运行。 But adding a duplicate key is too horrible to think about in mapping. 但是在映射中添加重复密钥实在太可怕了。 due to duplicate key the whole game is crashing many times. 由于重复的键,整个游戏崩溃了很多次。 Please help with some working code example. 请提供一些工作代码示例的帮助。

Thank you 谢谢
Rupali 鲁帕里

MapForUser1 and MapForUser2 are two different Maps so their respective "113" keys are two different data sets already but if having the same key name simplifies your code then you should use two data files, one for each user to allow for independent update. MapForUser1MapForUser2是两个不同的Map,因此它们各自的"113"键已经是两个不同的数据集,但是如果具有相同的键名可以简化代码,则应使用两个数据文件,每个用户一个,以允许独立更新。 File.WriteMap() writes in a sequential text file. File.WriteMap()写入顺序文本文件。 Without seeing more of your code it is very likely that handling file read/write operations and synchronizing it with two users causes the problem. 在看不到更多代码的情况下,很有可能处理文件读/写操作并将其与两个用户同步会导致此问题。

If you insist on using one data file it is better to use Random Access File. 如果您坚持使用一个数据文件,则最好使用随机访问文件。

RandomAccesFile.WriteObject(Object,Compress as Boolean,Position as Byte)

    Dim raf As RandomAccessFile
raf.initialize(File.DirRootExternal,"score.dat",False)

raf.WriteObject(MapForUser1,True,0)     'write from byte'
raf.WriteObject(MapForUser2,True,5000)  'write from byte 5000' 

raf.Close

Then to read: 然后阅读:

raf.initialize(File.DirRootExternal,"score.dat",False)


MapForUser1=raf.ReadObject(0)         'read from byte 0'
MapForUser2=raf.ReadObject(5000)       'read from byte 5000'

raf.Close

Take a look here: https://b4x.com/android/forum/threads/write-2-map-views-to-a-single-txt-file.48974/ 在这里看看: https : //b4x.com/android/forum/threads/write-2-map-views-to-a-single-txt-file.48974/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM