"SDK Assemblies Linking Only" issue with CursorLoader

I have this code to load list of contacts from device:

ICursor cursor = null;
try {
    cursor = (ICursor) new CursorLoader (activity, uri, projection, null, null, null).LoadInBackground ();

    if (cursor.MoveToFirst ()) {
        do {
            try {
                list.Add (new Contact (cursor.GetInt (cursor.GetColumnIndex (projection [0])), cursor.GetString (cursor.GetColumnIndex (projection [1]))));
            } catch (Exception e) {
                Log.Error ("TCH_error", "Contacts - GetListPermissionsDetermined - contact error:\n" + e.Message);
            }
        } while (cursor.MoveToNext ());
    }

    cursor.Close ();
} catch (Exception e) {
    Log.Error ("TCH_e", "Contacts - RequestPermissionsDone - Exception:\n" + e.StackTrace);

    Crashes.TrackError (e, new Dictionary<string, string> {
        {"FailureAt", "Cursor"}
    });
} finally {
    try {
        if (cursor != null) {
            cursor.Close ();
        }
    } catch (Exception e) {
        Log.Error ("TCH_e", "Contacts - RequestPermissionsDone - Exception:\n" + e.StackTrace);
    }
}

Everything works fine when I am debugging the app, but Release builds of the app show System.InvalidCastException: Specified cast is not valid error on some devices. For example on Samsung phones both Debug and Release works, but e.g. Honor 8 shows this error.
I tried to debug it changing build settings and found out that if I enable Linking to SDK Assemblies Only I get this error. But if I leave Linking to None, then there is no error.

Is this an issue with Xamarin? Or is there something I can do, other then having to not use linking?

Best Answer

Answers

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai
    edited December 2018

    In which line did this issue occurred? Please post the entire error trace.

  • tomaschylytomaschyly Member ✭✭
    edited December 2018

    I removed the try catch finally block to make sure I get the correct line and Visual Studio tells me it is on this line.

    ICursor cursor = (ICursor) new CursorLoader (activity, uri, projection, null, null, null).LoadInBackground ();
    

    This happens only when Linking - SDK Assemblies Only is enabled. If I do not do any Linking, then it works.

    Unhandled Exception:
    
    System.InvalidCastException: Specified cast is not valid. 
    
  • YorkGoYorkGo CNMember, Xamarin Team Xamurai
    edited January 1

    Did this issue only happened on Honor 8 device? Have you tested it on emulator?

    And would you mind sharing a basic demo that can reproduce the problem? Troubleshooting will be much convenient if we can reproduce this issue on our side. :)

  • tomaschylytomaschyly Member ✭✭

    Sorry, forgot to add, I retested it and it happens on all devices that I have access to. I noticed it first on Honor 8, but it happens on Samsungs with different Android versions.

    Dont know if I will have time today, but I will put something together and share it with you.

  • tomaschylytomaschyly Member ✭✭

    Here is demo project link

  • tomaschylytomaschyly Member ✭✭

    Thank you, it works. I should have searched for a solution like this sooner myself.

Sign In or Register to comment.