如何从Android Proto DataStore中的重复列表中删除项目?

发布于 2025-01-17 14:44:45 字数 1362 浏览 5 评论 0原文

这是带有字符串列表的定义的原始文件。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Oo萌小芽oO 2025-01-24 14:44:45

您能否发布 existingMessages 的确切数据类型。

我不是原始数据存储方面的专家,所以如果我错了,请纠正我。
根据异常消息,崩溃背后可能有几个主要原因:

  1. existingMessages 是不可变列表。根据异常,我们可以看到集合的名称是java.util.Collections$UnmodifyingCollection
  2. existingMessages 是一个自定义可变列表,尚未实现 #remove 函数。

要解决此问题,您可以根据您的指南选择以下方法之一:

  1. 将 Builder 类中的 messageList 类型更新为其他外部类的 MutableList。
  2. 如果您使用列表的自定义实现并且 #remove 未实现,请实现它。
  3. 如果您无法更改 messageList 的返回类型,只需使用 #filter 方法复制并跳过所需的消息,如下所示:
existingMessages.filter { it != msg }

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:

  1. The existingMessages is immutable list. As per the exception, we can see that the name of the collection is java.util.Collections$UnmodifiableCollection.
  2. The 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:

  1. Update the type of messageList in Builder class as MutableList for other external classes.
  2. In case you are using custom implementation of list and the #remove is not implemented, please implement it.
  3. In case you can't change the return type of messageList, simply use #filter method to copy and skip the required message as follow:
existingMessages.filter { it != msg }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文