[AC-6435] Hyperloop object Casting not working on Android
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | n/a |
Status | Open |
Resolution | Unresolved |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | Hyperloop |
Labels | n/a |
Reporter | Gabriel S. |
Assignee | Unknown |
Created | 2019-11-15T14:11:18.000+0000 |
Updated | 2020-01-21T12:24:23.000+0000 |
Description
I'm trying to use Firebase Firestore (a real-time database) through Hyperloop on Android by using the Firebase SDK.
When a Firestore Collection (query) is fetched ^1^, a generic Object is returned. In order to access data stored in this Object we must be able to execute its methods ^2^, such as
getDocuments()
or be able to iterate over it. As suggested by Hyperloop docs ^3^, the correct procedure is to "Cast" the generic Object in order to access its custom methods and attributes. But the following error shows up when attempting to cast a QuerySnaphot:
HyperloopProxy: (main) [985,7274] Unable to find matching constructor for class: com.google.firebase.firestore.QuerySnapshot, args: [com.google.firebase.firestore.QuerySnapshot@7341330]
Steps to reproduce:
Deploy a Firestore instance on Firebase, manually create a collection called "test" and 1 or 2 documents inside it. Install the following package available in the Maven repo using Gradle 'com.google.firebase:firebase-firestore:19.0.2'
Then run the following code on a Appcelerator Android app (there are several printouts in order to check the kind of objects we are dealing with):
var FirebaseApp = require('com.google.firebase.FirebaseApp');
var FirebaseOptions = require('com.google.firebase.FirebaseOptions');
var Activity = require('android.app.Activity');
var OnCompleteListener = require('com.google.android.gms.tasks.OnCompleteListener');
var FirebaseFirestore = require('com.google.firebase.firestore.FirebaseFirestore');
var activity = new Activity(Ti.Android.rootActivity);
var builder = new FirebaseOptions.Builder()
.setApplicationId('Your_ApplicationId')
.setApiKey('Your_setApiKey')
.setProjectId('Your_ProjectId')
.setGcmSenderId('Your_GcmSenderId');
FirebaseApp.initializeApp(
activity.getApplicationContext(),
builder.build(),
'test'
);
var firebaseAppInstance = FirebaseApp.getInstance('test');
var db = FirebaseFirestore.getInstance(firebaseAppInstance);
var docRef = db.collection('test')
docRef.get().addOnCompleteListener(new OnCompleteListener({
onComplete: function(task) {
if(task.isSuccessful()) {
var collection = task.getResult();
Ti.API.info('collection object');
Ti.API.info(collection)
Ti.API.info('collection getOwnPropertyNames');
Ti.API.info('----------\n');
Ti.API.info(Object.getOwnPropertyNames(collection));
Ti.API.info('----------\n');
Ti.API.info('collection values');
Ti.API.info(Object.values(collection));
Ti.API.info('for item in collection');
for (item in collection) {
Ti.API.info(item)
}
Ti.API.info('----------\n');
Ti.API.info('collection Array.from');
var arr = Array.from(collection);
Ti.API.info('collection for');
for (var i = 0, j = arr.length; i < j; i++) {
Ti.API.info(arr[i])
}
Ti.API.info('----------\n');
Ti.API.info('Try casting')
var QuerySnapshot = require('com.google.firebase.firestore.QuerySnapshot');
querySnapshot = new QuerySnapshot(collection);
Output:
[INFO] : ----------
[INFO] : collection object
[INFO] : com.google.firebase.firestore.QuerySnapshot@7341330
[INFO] : collection getOwnPropertyNames
[INFO] : ----------
[INFO] : $native,_hasPointer,_private
[INFO] : ----------
[INFO] : collection values
[INFO] : [object Instance],true,[object Object]
[INFO] : for item in collection
[INFO] : $native
[INFO] : _hasPointer
[INFO] : _private
[INFO] : className
[INFO] : getClass
[INFO] : wait
[INFO] : hashCode
[INFO] : equals
[INFO] : notifyAll
[INFO] : clone
[INFO] : toString
[INFO] : finalize
[INFO] : notify
[INFO] : ----------
[INFO] : collection Array.from
[INFO] : collection for
[INFO] : ----------
[INFO] : Try casting
[ERROR] : HyperloopProxy: (main) [985,7274] Unable to find matching constructor for class: com.google.firebase.firestore.QuerySnapshot, args: [com.google.firebase.firestore.QuerySnapshot@7341330]
As a workaround, I tried to access the documents inside this collection using 'com.google.common.collect.Lists' (which converts an iterable to a Java array). But the documents are generic Objects as well and the same error shows up when trying to cast them.
References:
1 - https://firebase.google.com/docs/firestore/query-data/get-data#get_multiple_documents_from_a_collection
2 - https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/QuerySnapshot
3 - https://wiki.appcelerator.org/display/guides2/Android+Hyperloop+Programming+Guide#AndroidHyperloopProgrammingGuide-Casting
Shouldn't this issue be assigned to somebody?