Bug 17081 - Mono.Data.SQLite requires instance method for callback, but Xamarin.iOS requires a static method.
Summary: Mono.Data.SQLite requires instance method for callback, but Xamarin.iOS requi...
Status: RESOLVED DUPLICATE of bug 16542
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 7.0.2.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: 7.2.1
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-01-06 19:43 UTC by Jon Goldberger [MSFT]
Modified: 2014-03-12 17:55 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED DUPLICATE of bug 16542

Comment 1 Jon Goldberger [MSFT] 2014-01-06 19:45:10 UTC
I am currently working on an iOS app which requires custom SQLite
collations. We have the same app for Android, and I used Mono.Data.Sqlite
for both projects. I have created classes for my collations (deriving from
SqliteFunction) and added the [SqliteFunction(FuncType =
FunctionType.Collation, Name = ".")] attribute to them.

When I run the app on the iOS simulator it works, but when I try to run the
app on a device an exception is shown: "Attempting to JIT compile method [.]
Mono.Data.Sqlite.SqliteFunction:CompareCallback16"

On the iOS Limitations page
(http://docs.xamarin.com/guides/ios/advanced_topics/limitations/) it says
that I have to use static methods for callbacks, but Mono.Data.Sqlite
requires the collection compare callback to be an instance method.



Do you know another way I can register the collations using static callback
methods?
---------------------------------------------------------------------
I have attached a small test case to demonstrate my problem. The way I see it (correct my if I’m wrong), the Mono.Data.Sqlite framework wants the callback methods of a custom sqlite collation to be instance methods of a class derived from SqliteFunction, whereas the Xamarin.iOS framework requires them to be static methods.



The test case contains a button which tries to open a new sqlite connection. The Mono.Data.Sqlite framework automatically registers the TestCollation sqlite collation class and its callback method when a connection is opened, and the registration fails on iOS devices. So if I run the test case on the iOS Simulator it works fine, but if I run it on a real device it fails.

------------------------------------------------------------------------
Comment 3 Rolf Bjarne Kvinge [MSFT] 2014-01-07 11:17:00 UTC
This the same as this bug: https://bugzilla.novell.com/show_bug.cgi?id=679561

In reality we'll have to rewrite our support for custom functions in Mono.Data.SQLite so that the callbacks are static and have the proper [MonoPInvokeCallback] attributes.
Comment 4 Dominik Weber 2014-01-14 16:21:50 UTC
I implemented a workaround to use static callbacks by calling Mono.Data.Sqlite.SQLite3.CreateCollation() using reflection. To increase the usability of the framework I would suggest to introduce a public method CreateCollation() on the SqliteConnection class so the developer can register static method callbacks.
Another suggestion is to move the method Mono.Data.Sqlite.SQLite3_UTF16.UTF16ToString() to the SqliteConvert class because the developer has to use this method when implementing a static callback method to handle UTF16 data.

These are my static collation callbacks:

        [MonoPInvokeCallback(typeof(SQLiteCollation))]
        public static int CompareCallbackUnicode(IntPtr ptr, int len1, IntPtr ptr1, int len2, IntPtr ptr2)
        {
            return unicodeStrategy.Compare(UTF8ToString(ptr1, len1), UTF8ToString(ptr2, len2));
        }

        [MonoPInvokeCallback(typeof(SQLiteCollation))]
        public static int CompareCallbackUnicode16(IntPtr ptr, int len1, IntPtr ptr1, int len2, IntPtr ptr2)
        {
            return unicodeStrategy.Compare(UTF16ToString(ptr1, len1), UTF16ToString(ptr2, len2));
        }

Calling the Mono.Data.Sqlite.SQLite3.CreateCollation() method is a bit complicated because the parameters for this method are of type SQLiteCollation which is defined internally. If the CreateCollation method (and its parameter types) would be publicly visible it would be much easier for a developer to implement custom callbacks.
Comment 5 Rolf Bjarne Kvinge [MSFT] 2014-02-06 08:49:27 UTC

*** This bug has been marked as a duplicate of bug 16542 ***