简体   繁体   中英

How can I ensure consistency between multiple model object instances

BACKGROUND

This is a problem I keep coming back to as I'm developing an Android application. Let's say my app lets users "follow" TV shows. My TVShow model class is as follows:

public class TVShow {
    String title;
    String[] show_cast;
    boolean currently_airing = true;
    boolean following = false;
    long nextEpisodeTime;
    String genre;
}

I have an SQL database that stores hundreds of TV shows. This is useful because I can leverage detailed queries to select shows based on specific information. The problem I get is when I have multiple instances of TVShow that represent the same show.

EXAMPLE

Let's say I have the TV show "The Walking Dead" in my database and I create a TVShow object from an SQL query. This object instance will be called TVShow#1 . I decide to "follow" this show, so I set TVShow#1.following = true . Somewhere else in my app I query a list of shows that are currently airing, which "The Walking Dead" is part of. A new instance of TVShow will be created called TVShow#2 for that list. The problem here is that I have two TVShow objects, TVShow#1 and TVShow#2 that represent "The Walking Dead", but they have differing values for following . This will cause problems throughout the logic of my application because there isn't a consistency between all TVShow instances that represent the same TV show.

MY BAD WORKAROUND

Expand this to a bunch of object instances and much more complicated fields and logic, and that is the situation I am currently in. I have mitigated this by loading all SQL data into memory (usually ArrayList or HashMap ) when the app is launched, and only querying these Java objects instead of the SQL database. This solution is inefficient and inconvenient, where SQL queries could really help. I hope my examples were not too confusing. If I can clarify or provide more information, I'd be happy to. Please let me know if there is something better I can do to ensure consistency between multiple instances of the same model object.

It sounds like the "following" property should be the property of a user rather than a TV show.

Your database should record the association between users and which shows they are following. So, if a user decides to follow a show, you save a persistent association in the database between that user and that show, and then the next time you query the database the state will be consistent.

The solution I use for this problem is RxJava based, however it would be possible to implement without Rx.

The idea is that you 'subscribe' to the data from your database query. Each time there is a write to the database which affects one of the tables your query touches then the query gets re-run and you get a fresh set of data.

This way when TVShow1 gets saved to the database, the query which produced TVShow2 gets re-run and you have the latest version of your data.

There are two libraries which do all of this for you if you are using Rx:

StorIO
sqlbrite

If you are not using Rx then Realm has a similar functionality with its change adapters.

Finally you could roll your own. This would involve having a singleton which you register your queries with, linked to the tables they are reading from. Each time you do a write you let this class know which tables have been changed and it re-runs your queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM