使用FirebaseRecyclerViewAdapter

时间:2015-12-31 18:44:50

标签: java android firebase out-of-memory nosql

我正在构建一个用于播放有声读物的Android应用程序。有声书籍元数据(标题,作者等)使用以下结构存储在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 个答案:

答案 0 :(得分:1)

问题确实可能是因为您正在加载大量关于每本书的不必要数据。特别是在移动设备上,您应该只加载要显示给用户的数据。由于Firebase始终加载整个节点,因此您必须建模数据,以便只加载所需的数据。

解决方案是将音轨与有声书的其他信息分开。这称为denormalizing the data,是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的单独顶级实体(这是在数据库中建模的方式)。