使用 FirebaseRecyclerViewAdapter 时出现 OutOfMemoryError

3

我正在开发一个播放有声读物的安卓应用。这些有声读物的元数据(如标题、作者等)存储在Firebase数据库中,使用以下结构:

root
  --- audioBooks
    --- <individual hash for each book>
      --- author: "Ray Bradbury"
      --- title: "Fahrenheit 451"
      --- finished: false
      --- key: <individual hash for each book>
      --- cover
        --- b64Cover: <b64-encoded image>
        --- backgroundColor: -14473711
        --- ...
      --- tracks
        --- 0
          --- title: "Track 1"
          --- duration: 361273
          --- finished: false
          --- currentPosition: 12345
          --- ...
        --- 1
          --- ...
      --- currentTrack
        --- title: "Track 1"
        --- duration: 361273
        --- finished: false
        --- currentPosition: 12345
        --- ...

我有三个模型类,分别是AudioBook、AudioBookTrack和AudioBookCover(为了更好地概览,已删除setter和getter)。

public class AudioBook {
    private String title;
    private String author;
    private int duration;
    private boolean finished;
    private String key;
    private AudioBookCover cover;
    private ArrayList<AudioBookTrack> tracks;
    private AudioBookTrack currentTrack;

    public AudioBook() {
    }

    public AudioBook(String title, String author, boolean finished, String key, AudioBookCover cover, ArrayList<AudioBookTrack> tracks, AudioBookTrack currentTrack) {
        this.title = title;
        this.author = author;
        this.finished = finished;
        this.key = key;
        this.cover = cover;
        this.tracks = tracks;
        this.currentTrack = currentTrack;
    }
}

public class AudioBookTrack {
    private String title;
    private String album;
    private String author;
    private String filePath;
    private int duration;
    private int currentPosition;
    private int index;
    private boolean finished;
    private String key;

    public AudioBookTrack() {
    }

    public AudioBookTrack(String title, String album, String author, String filePath, int duration, int currentPosition, int index, boolean finished, String key) {
        this.title = title;
        this.album = album;
        this.author = author;
        this.filePath = filePath;
        this.duration = duration;
        this.currentPosition = currentPosition;
        this.index = index;
        this.finished = finished;
        this.key = key;
    }
}

public class AudioBookCover {
    private String b64Cover;
    private int backgroundColor;
    private int primaryColor;
    private int secondaryColor;
    private int detailColor;

    public AudioBookCover() {
    }

    public AudioBookCover(String b64Cover, int backgroundColor, int primaryColor, int secondaryColor, int detailColor) {
        this.b64Cover = b64Cover;
        this.backgroundColor = backgroundColor;
        this.primaryColor = primaryColor;
        this.secondaryColor = secondaryColor;
        this.detailColor = detailColor;
    }
}

在Firebase数据库中消耗数据并将其转换为模型类没有问题。但是,我现在想使用FirebaseRecyclerViewAdapter在RecyclerView中列出所有音频书籍。这是代码,在片段的onCreateView()方法中执行:

mAudioBooksList = (RecyclerView) view.findViewById(R.id.audiobooks_list);
mAudioBooksList.setHasFixedSize(true);

ref = new Firebase(Constants.FIREBASE_URL).child("audioBooks");

mAdapter = new FirebaseRecyclerViewAdapter<AudioBook, AudioBooksViewHolder>(AudioBook.class, R.layout.audiobook_grid_item, AudioBooksViewHolder.class, ref) {
    @Override
    public void populateViewHolder(AudioBooksViewHolder audioBooksViewHolder, AudioBook audioBook) {
        audioBooksViewHolder.author.setText(audioBook.getAuthor());
        audioBooksViewHolder.title.setText(audioBook.getTitle());
        try {
            byte[] byteArray = Base64.decode(audioBook.getCover().getB64Cover());
            Bitmap coverBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            audioBooksViewHolder.cover.setImageBitmap(coverBitmap);
        }
        catch(IOException e) {
            Log.e(TAG, "Could not decode album cover: " + e.getMessage());
        }
    }
};
mAudioBooksList.setAdapter(mAdapter);

这是ViewHolder:

private static class AudioBooksViewHolder extends RecyclerView.ViewHolder {
TextView author;
TextView title;
ImageView cover;

public AudioBooksViewHolder(View itemView) {
    super(itemView);
    author = (TextView)itemView.findViewById(R.id.audiobook_grid_author);
    title = (TextView)itemView.findViewById(R.id.audiobook_grid_title);
    cover = (ImageView) itemView.findViewById(R.id.audiobook_grid_cover);
}

现在这段代码基本可以运行(我可以看到屏幕上显示的值),然而很快,应用程序由于内存不足错误而崩溃:

Uncaught exception in Firebase runloop (2.4.0). Please report to support@firebase.com
    java.lang.OutOfMemoryError: Failed to allocate a 35116844 byte allocation with 16773184 free bytes and 32MB until OOM
        at java.lang.StringFactory.newStringFromChars(Native Method)
        at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:629)
        at java.lang.StringBuilder.toString(StringBuilder.java:663)
        ...

我在一台物理的Nexus 5上测试了这个应用程序(没有模拟器)。 我猜想这是因为我的嵌套模型(AudioBooks内包含AudioBookTracks)导致了几百个Java对象。不幸的是,我不知道如何解决这个问题。如果只需要列出音频书籍,我就不需要它们对应的音频书籍轨迹,但由于音频书籍轨迹是音频书籍的子元素,它们也被转换为 Java 对象。 另一方面,我还尝试通过使用 ref.limitToFirst(2) 来限制显示的音频书籍数量。然而,应用程序仍会崩溃,但直到崩溃需要更长的时间。

您是否看到了导致问题的代码问题?或者是否有可能仅从Firebase数据库中选择我需要的值,跳过例如音频书籍轨迹之类的内容?

谢谢!

1个回答

1
问题可能是由于您正在加载有关每本书的大量不必要数据造成的。特别是在移动设备上,您应该只加载将要显示给用户的数据。由于Firebase始终会加载整个节点,因此您需要对数据进行建模,以便仅加载所需的数据。
解决方案是将音频书籍的轨道与其他信息分开。这被称为 数据去规范化,在NoSQL数据库中非常常见。
规范化数据将存储为:
root
  --- audioBooks
    --- <individual hash for each book>
      --- author: "Ray Bradbury"
      --- title: "Fahrenheit 451"
      --- finished: false
      --- key: <individual hash for each book>
      --- cover
        --- b64Cover: <b64-encoded image>
        --- backgroundColor: -14473711
        --- ...
  --- audioBookTracks
    --- <individual hash for each book>
      --- 0
        --- title: "Track 1"
        --- duration: 361273
        --- finished: false
        --- currentPosition: 12345
        --- ...
        --- 1
        --- ...
    --- currentTrack
      --- title: "Track 1"
      --- duration: 361273
      --- finished: false
      --- currentPosition: 12345
      --- ...

关于一本书的信息以及曲目都存储在同一个ID下(您所称的“每本书的独立哈希”),但位于不同的顶级节点下。有关书本本身的数据位于audioBooks下,而曲目列表位于authBookTracks下。

现在,您可以使用以下方法分别加载书籍信息或该书籍的曲目:

ref.child("audioBooks").child(bookId).addValueEventListener(...

或者

ref.child("audioBookTracks").child(bookId).addChildEventListener(...

你可以将AudioBook Java类建模为保留其tracks成员但不进行序列化(使用Jackson注释)。但是,你也可以将书籍及其曲目列表视为独立的顶级实体,这些实体恰好具有相同的ID(这是在数据库中建模的方式)。

非常感谢!我按照您的建议更改了数据模型,现在它可以正常工作了。 - BGoll

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接