如何从Android Proto DataStore中的重复列表中删除项目?
这是带有字符串列表的定义的原始文件。
message MyMessages {
repeated string message = 1;
}
这是从重复消息列表中删除消息的功能。
suspend fun removeMsg(msg : String) {
myMessagesStore.updateData { myMessages ->
val existingMessages = myMessages.toBuilder().messageList;
if (existingMessages.contains(msg)) {
existingMessages.remove(msg)
myMessages.toBuilder().clear().addAllMessage(existingMessages).build()
} else {
myMessages
}
}
}
当上述代码运行时,它崩溃了astarenMessages.Remove(msg)
带有错误:
java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.remove
现有MESSAGES是一个包含删除函数的mutablelist,为什么此崩溃在此上崩溃?来自原始数据存储中重复列表项目的项目?
更新: 现有的消息是,
/**
* <code>repeated string message = 1;</code>
* @return A list containing the message.
*/
@java.lang.Override
public java.util.List<java.lang.String>
getMessageList() {
return java.util.Collections.unmodifiableList(
instance.getMessageList());
}
看起来原始类的生成类正在使重复的列表Umodfiablelist。那我的问题是为什么没有myMessages.tobuilder()。从生成类中的ememovemessage(newMessage)
函数类似于AddMessage函数,例如:
myMessages.toBuilder().addMessage(newMessage)
Here is the proto file with the definition for a list of strings.
message MyMessages {
repeated string message = 1;
}
Here is the function for removing a message from the repeated message list.
suspend fun removeMsg(msg : String) {
myMessagesStore.updateData { myMessages ->
val existingMessages = myMessages.toBuilder().messageList;
if (existingMessages.contains(msg)) {
existingMessages.remove(msg)
myMessages.toBuilder().clear().addAllMessage(existingMessages).build()
} else {
myMessages
}
}
}
When the above code runs, it crashed on existingMessages.remove(msg)
with the error:
java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.remove
existingMessages is a MutableList which contains the remove function, why is this crashing on this and what's the proper way to remove an item from a repeated list item in proto DataStore?
Updates:
existingMessages is
/**
* <code>repeated string message = 1;</code>
* @return A list containing the message.
*/
@java.lang.Override
public java.util.List<java.lang.String>
getMessageList() {
return java.util.Collections.unmodifiableList(
instance.getMessageList());
}
It looks like the generated class from proto is making the repeated list unmodfiableList. Then my question is why there isn't a myMessages.toBuilder().removeMessage(newMessage)
function from the generated class similar to the addMessage function such as this:
myMessages.toBuilder().addMessage(newMessage)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否发布
existingMessages
的确切数据类型。我不是原始数据存储方面的专家,所以如果我错了,请纠正我。
根据异常消息,崩溃背后可能有几个主要原因:
existingMessages
是不可变列表。根据异常,我们可以看到集合的名称是java.util.Collections$UnmodifyingCollection
。existingMessages
是一个自定义可变列表,尚未实现#remove
函数。要解决此问题,您可以根据您的指南选择以下方法之一:
messageList
类型更新为其他外部类的 MutableList。#remove
未实现,请实现它。messageList
的返回类型,只需使用#filter
方法复制并跳过所需的消息,如下所示:Could you please post the exact data type of
existingMessages
.I am not an expert in proto DataStore, so please correct me incase I am wrong.
As per the exception message, there could be few main reason behind the crash:
existingMessages
is immutable list. As per the exception, we can see that the name of the collection isjava.util.Collections$UnmodifiableCollection
.existingMessages
is a custom mutable list, which haven't implemented the#remove
function.To fix this, you can choose one of the following approach based on your guidelines:
messageList
in Builder class as MutableList for other external classes.#remove
is not implemented, please implement it.messageList
, simply use#filter
method to copy and skip the required message as follow: