Room 中的多态实体

Polymorphic entities in Room

我的房间数据库中有 3 个实体:

AlbumPhotosMediaItemVideosMediaItem.

VideosMediaItemPhotosMediaItem 继承自 MediaItem

MediaItem 不是 DB 中的实体,它只是一个抽象基础 class.

我想创建一个查询,returns 特定相册中的所有照片和视频媒体项目根据其创建日期降序排列。

因此查询将创建一个 MediaItems 列表,但具有派生类型。 (PhotoMediaItemVideoMediaItem)以多态方式。

这是我试过的方法:

    @Query("SELECT * FROM PhotosMediaItem WHERE PhotosMediaItem = :albumId " +
        "UNION SELECT * FROM VideosMediaItem WHERE VideosMediaItem = :albumId" +
        " ORDER by CreationDate DESC")
    List<MediaItem> getAllMediaInAlbum(int albumId);

这显然行不通,因为它试图启动 MediaItem 对象,这不是我的意图。我希望此查询启动派生的 class、PhotoMediaItemVideoMediaItem

这是我的查询在迁移到 Room 之前的样子,使用常规的 SQLiteHelper,它工作得很好:

public ArrayList<MediaItem> getMediaListByAlbumId(int palbumId)
{
    Cursor cursor = null;
    try{
        ArrayList<MediaItem> mediaList = new ArrayList<>();
        String selectQuery = "SELECT "+ mPhotoId +","+ mPhotoCreationDate +", 0 AS mediaType, '' FROM "+ mPhotosTableName + " WHERE " + this.mPhotoAlbumId + "="+palbumId +
                " UNION " +
                "SELECT "+ mVideoId +","+ mVideoCreationDate + " ,1 AS mediaType, " + mVideoLength + " FROM " + mVideosTableName + " WHERE " + this.mVideoAlbumId +"="+palbumId +
                " ORDER BY CreationDate DESC";
        cursor = mDB.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()){
            do {
                // MediaHolder consists of the media ID and its type
                int mediaType = cursor.getInt(2);
                MediaItem mediaItem = null;
                if (mediaType == 0) {
                    mediaItem = new PhotoMediaItem(cursor.getInt(0), null, palbumId);
                } else if (mediaType == 1) {
                    mediaItem = new VideoMediaItem(cursor.getInt(0), null, palbumId, cursor.getLong(3));
                }
                mediaList.add(mediaItem);
            }
            while (cursor.moveToNext());
        }
        return mediaList;
    }
    finally  {
        if(cursor != null){
            cursor.close();
        }
    }

}

那我怎么用Room达到同样的效果呢?

我认为您在这里有多种选择:

选项 1

您使用单个 table 来存储所有 MediaItem,并使用鉴别器列来区分视频和照片。 您有一个执行查询的 DAO 方法,应用 order by 和 return 一个 Cursor。然后你可以使用你现有的游标操作逻辑来 return a List<MediaItem> 它可以看起来像这样:

@Dao
public abstract class MediaItemDao() {

    @Query("you query here")
    protected Cursor getByAlbumIdInternal(int albumId);

    public List<MediaItem> getByAbumId(int albumId) {
        Cursor cursor = null;
        try{
            List<MediaItem> mediaList = new ArrayList<>();
            cursor = getByAlbumIdInternal(albumId);
            // looping through all rows and adding to list
            if (cursor.moveToFirst()){
                do {
                    // use the discriminator value here
                    int mediaType = cursor.getInt(cursor.getColumnIndex("you discriminator column name here"));
                    MediaItem mediaItem = null;
                    if (mediaType == 0) {
                        mediaItem = new PhotoMediaItem(cursor.getInt(0), null, palbumId);
                    } else if (mediaType == 1) {
                        mediaItem = new VideoMediaItem(cursor.getInt(0), null, palbumId, cursor.getLong(3));
                    }
                    mediaList.add(mediaItem);
                } while (cursor.moveToNext());
            }
            return mediaList;
        }
        finally  {
            if(cursor != null){
                cursor.close();
            }
        }
    }
}

选项 2

您使用两个不同的 table 来存储 VideosMediaItemPhotosMediaItem。您有一个 MediaItemDao,它有两个执行查询的内部方法和一个 public 方法,它将两个结果集合并在一起并在 java 代码中应用排序。它可以看起来像这样:

@Dao
public abstract class MediaItemDao() {

    @Query("your query to get the videos, no order by")
    protected List<VideoMediaItem> getVideosByAlbumId(int albumId);

    @Query("your query to get the photos, no order by")
    protected List<PhotosMediaItem> getPhotosByAlbumId(int albumId);

    @Transaction
    public List<MediaItem> getByAlbumId(int albumId) {
        final List<MediaItem> mediaItems = new LinkedList<>();
        mediaItems.add(getVideosByAlbumId(albumId));
        mediaItems.add(getPhotosByAlbumId(albumId));
        Collections.sort(mediaItems, <you can add a comparator here>);
        return mediaItems;
    }
}

编辑:如何为此选项利用实时数据?

正如我提到的,您应该使用 LiveData 作为受保护方法的 return 类型,这样您就可以收到有关这些 table 的基础更改的通知。所以它们应该是这样的:

protected LiveData<List<VideoMediaItem>> getVideosByAlbumId(int albumId);

protected LiveData<List<PhotosMediaItem>> getPhotosByAlbumId(int albumId);

为了 return 将单个 LiveData 发送到客户端,您应该将这两种方法的输出压缩到一个流中。您可以使用自定义 MediatorLiveData 实现来实现此目的。它可能看起来像这样:

public class ZipLiveData<T1, T2, R> extends MediatorLiveData<R> {

    private T1 mLastLeft;
    private T2 mLastRight;
    private Zipper<T1, T2, R> mZipper;

    public static final <T1, T2, R> LiveData<R> create(@NonNull LiveData<T1> left, @NonNull LiveData<T2> right, Zipper<T1, T2, R> zipper) {
        final ZipLiveData<T1, T2, R> liveData = new ZipLiveData(zipper);
        liveData.addSource(left, value -> {
            liveData.mLastLeft = value;
            update();
        });
        liveData.addSource(right, value -> {
            liveData.mLastRight = value;
            update();
        });
        return liveData;
    }

    private ZipLiveData(@NonNull Zipper<T1, T2, R> zipper) {
        mZipper = zipper;
    }

    private update() {
        final R result = zipper.zip(mLastLeft, mLastRight);
        setValue(result);
    }

    public interface Zipper<T1, T2, R> {

        R zip(T1 left, T2 right);

    }
}

然后你只需在你的存储库中使用它 public 方法如下:

public List<MediaItem> getByAlbumId(int albumId) {
    return ZipLiveData.create(
        getVideosByAlbumId(albumId),
        getPhotosByAlbumId(albumId),
        (videos, photos) -> {
            final List<MediaItem> mediaItems = new LinkedList<>();
            mediaItems.add(videos);
            mediaItems.add(photos);
            Collections.sort(mediaItems, <you can add a comparator here>);
            return mediaItems;
        }
}

选项 3

这仅适用于您拥有存储库层的情况。

您使用两个不同的 table 来存储 VideosMediaItemPhotosMediaItem。每个人都有一个 DAO class。你有一个存储库,它依赖于两个 DAO 并组合结果集,应用排序。它可以看起来像这样:

@Dao
public abstract class VideosMediaItemDao {

    @Query("your query to get the videos, no order by")
    public List<VideoMediaItem> getByAlbumId(int albumId);

}

@Dao
public abstract class PhotosMediaItemDao {

    @Query("your query to get the photos, no order by")
    public List<PhotosMediaItem> getByAlbymId(int albumId);

}

public interface MediaItemRepository {

    public List<MediaItem> getByAlbumId(int albumId);

}

class MediaItemRepositoryImpl {

    private final VideosMediaItemDao mVideoDao;
    private final PhotosMediaItemDao mPhotoDao;

    MediaItemRepositoryImpl(VideosMediaItemDao videoDao, PhotosMediaItemDao photoDao) {
        mVideoDao = videoDao;
        mPhotoDao = photoDao;
    }

    @Override
    public List<MediaItem> getByAlbumId(int albumId) {
        final List<MediaItem> mediaItems = new LinkedList<>();
        mediaItems.add(mVideoDao.getByAlbumId(albumId));
        mediaItems.add(mPhotoDao.getByAlbumId(albumId));
        Collections.sort(mediaItems, <you can add a comparator here>);
        return mediaItems;
    }

}

我试了一下,似乎找到了一个将多个 LiveData 源压缩在一起的通用解决方案。

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData

class ZipLiveData<S>(private val process: (List<S>) -> List<S>) : MediatorLiveData<List<S>>() {
    val map = hashMapOf<String, List<S>>()
    fun addSource(source: LiveData<*>) {
        addSource(source) {
            map[source.toString()] = it as List<S>
            value = process(map.values.flatten())
        }
    }
}

和用法:

    @Transaction
    fun findItems(albumId: Int): LiveData<List<MediaItem>> {
        val liveData = ZipLiveData<MediaItem> { it.sortedBy { item -> item.weight } }
        liveData.addSource(getVideosByAlbumId(albumId))
        liveData.addSource(getPhotosByAlbumId(albumId))
        liveData.addSource(getSoundsByAlbumId(albumId))
        return liveData
    }

不确定这是否是最优雅的解决方案