Code Monkey home page Code Monkey logo

Comments (13)

b12kab avatar b12kab commented on August 11, 2024 2

@Zhuinden You were quite right - turning off the animation did the trick. I've updated the source code above to show the original instantiation of the adapter with animation = true.

Why the crash shows
columnIndex is less than 0
instead of
Animating the results requires a primaryKey
Is really very odd - and a likely defect.

The question why animation needs a primary key is a odd but separate question.

from realm-recyclerview.

Zhuinden avatar Zhuinden commented on August 11, 2024

Do you have a @PrimaryKey?

from realm-recyclerview.

thorbenprimke avatar thorbenprimke commented on August 11, 2024

@matbrandao Could you add a stacktrace?

from realm-recyclerview.

nolsigan avatar nolsigan commented on August 11, 2024

I had the same problem when I tried to query from realmObject by findAll()

RealmResults<Alarm> alarms = realm
                .where(Alarm.class)
                .findAll();

I don't know if it's a same problem, but I resolved it by using findAllSorted() instead of findAll()

from realm-recyclerview.

b12kab avatar b12kab commented on August 11, 2024

I'm getting this error now. My realm class doesn't have a PrimaryKey, as it's a child in a parent / child relationship. I want to get all of the items that belong to the parent and show them.

According to Realm developer (although this is for an iOS answer), PrimaryKey's aren't needed. https://stackoverflow.com/questions/32322460/should-i-define-the-primary-key-for-each-entity-in-realm

Extrapolating the above comment from @Zhuinden , does this mean it is required for realm-recyclerview?

from realm-recyclerview.

Zhuinden avatar Zhuinden commented on August 11, 2024

@b12kab technically primary keys shouldn't be necessary, and findAll() should work just fine. I got a similar error back a long day ago when I tried setting a RealmList that hadn't been copied to the Realm, but that was a while ago.

I'd have to know the code, really. A complete stack trace would be useful too, and Realm version.

EDIT: oh wait, this is the thorbenprimke realm RecyclerView.

Yes, you should have a string or long primary key to use this library with your RealmObject.

from realm-recyclerview.

b12kab avatar b12kab commented on August 11, 2024

Just in case @thorbenprimke might want it.

public class RealmReview extends RealmObject {
    @Index
    int parentId;

    String id;
    String author;
    String content;
    String url;
    Date dateAdded;
    Date dateUpdated;
}

Calling the Adapter:
mAdapter = new AdapterRecyclerViewRealmMovieReviews(mContext, mMovieResults, true, true);

Adapter:

public class AdapterRecyclerViewRealmReview extends RealmBasedRecyclerViewAdapter<RealmReview,
             AdapterRecyclerViewRealmReview.ViewReviewHolder> {


    private Context mContext = null;


    public AdapterRecyclerViewRealmReview (
Context context, RealmResults<RealmReview> realmResults,
 boolean automaticUpdate, boolean animateIdType) {

        super(context, realmResults, automaticUpdate, animateIdType);

        resetContext(context);

    }
<stuff>
    void resetContext(Context context) {
        if (context != null) {
            mContext = context;
        }
    }
}

Crash

                                                                                            --------- beginning of crash
07-23 18:46:21.663 8822-8822/net.springtale.realmviewholderexperiment E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                            Process: net.springtale.realmviewholderexperiment, PID: 8822
                                                                                            java.lang.ArrayIndexOutOfBoundsException: columnIndex is less than 0.
                                                                                                at io.realm.internal.TableView.nativeGetColumnType(Native Method)
                                                                                                at io.realm.internal.TableView.getColumnType(TableView.java:173)
                                                                                                at io.realm.RealmBasedRecyclerViewAdapter.<init>(RealmBasedRecyclerViewAdapter.java:172)
                                                                                                at io.realm.RealmBasedRecyclerViewAdapter.<init>(RealmBasedRecyclerViewAdapter.java:133)
                                                                                                at io.realm.RealmBasedRecyclerViewAdapter.<init>(RealmBasedRecyclerViewAdapter.java:123)
                                                                                                at net.springtale.realmviewholderexperiment.AdapterRecyclerViewRealmReview.<init>(AdapterRecyclerViewRealmReview.java:29)
                                                                                                at net.springtale.realmviewholderexperiment.FragmentMovieDetailReviews.fixAdapter(FragmentMovieDetailReviews.java:222)
                                                                                                at net.springtale.realmviewholderexperiment.FragmentMovieDetailReviews.resetMovieIdDetails(FragmentMovieDetailReviews.java:280)
                                                                                                at net.springtale.realmviewholderexperiment.FragmentMovieDetailReviews.onCreateView(FragmentMovieDetailReviews.java:175)
                                                                                                at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                                                                                                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                                                                                                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
                                                                                                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
                                                                                                at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
                                                                                                at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
                                                                                                at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                                                                                                at android.support.v4.view.ViewPager.populate(ViewPager.java:1235)
                                                                                                at android.support.v4.view.ViewPager.populate(ViewPager.java:1083)
                                                                                                at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1609)
                                                                                                at android.view.View.measure(View.java:18788)
                                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                                                at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                                                at android.view.View.measure(View.java:18788)
                                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                                                at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                                                at android.view.View.measure(View.java:18788)
                                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                                                at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                                                at android.view.View.measure(View.java:18788)
                                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                                                at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                                                at android.view.View.measure(View.java:18788)
                                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                                                at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                                                at android.view.View.measure(View.java:18788)
                                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                                                at android.view.View.measure(View.java:18788)
                                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                                                at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                                                at android.view.View.measure(View.java:1878

from realm-recyclerview.

Zhuinden avatar Zhuinden commented on August 11, 2024

Well if you look at the source, you are crashing on this line

https://github.com/thorbenprimke/realm-recyclerview/blob/master/library/src/main/java/io/realm/RealmBasedRecyclerViewAdapter.java#L172

Which is

    this.animateResults = (automaticUpdate && animateResults);
    if (animateResults) {
        animatePrimaryColumnIndex = realmResults.getTable().getTable().getPrimaryKey();
        if (animatePrimaryColumnIndex == TableOrView.NO_MATCH) {
            throw new IllegalStateException(
                    "Animating the results requires a primaryKey.");
        }
        animatePrimaryIdType = realmResults.getTable().getColumnType(animatePrimaryColumnIndex);

So you are trying to animate your results, but you don't have enough a primary key.

You can fix this by changing id to be primary key, or by disabling animating results.

from realm-recyclerview.

Zhuinden avatar Zhuinden commented on August 11, 2024

@b12kab reading the source of Table, it quite obviously returns -2 specified as NO_PRIMARY_KEY, but the condition here is for NO_MATCH which is -1.

https://github.com/realm/realm-java/blob/master/realm/realm-library/src/main/java/io/realm/internal/Table.java#L648

By the way, the PK is required to match the two retained results against one another and determine what element changed from where to where, and to identify the elements (to determine where they moved), their ID is required.

from realm-recyclerview.

Zhuinden avatar Zhuinden commented on August 11, 2024

Your id field in your RealmObject though, why isn't it a primary key?

from realm-recyclerview.

b12kab avatar b12kab commented on August 11, 2024

In my current use, I don't care what the id value is - I'm only using the parentId (1 to n objects with the same parentId). I'm not positive that the review id is always unique, so I don't want to take a chance with a duplicated primary key using the id field.

In this particular case, I don't actually need that animation functionality.

from realm-recyclerview.

Zhuinden avatar Zhuinden commented on August 11, 2024

I see, that makes sense. If it is not guaranteed to be unique, then it shouldn't be a primary key!

anyways, I think we've solved the case :)

from realm-recyclerview.

Zhuinden avatar Zhuinden commented on August 11, 2024

@thorbenprimke

from realm-recyclerview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.