I was looking in the following google documentation of firestore and especially on the part of updating an array. they mention:
Update elements in an array
If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present. arrayRemove() removes all instances of each given element.
problem is, Android studio can't resolve the method arrayUnion.
relevant part of the code:
public void onClick(View view) {
if(isStringValid(newListName.getText().toString())){
currListName = newListName.getText().toString().trim();
itemList mList = new itemList(currListName , mAuth.getCurrentUser().getEmail());
Map<String , Object> listMap = new HashMap<>();
listMap.put(KEY_LIST_NAME , currListName);
listMap.put(KEY_OWNER , mAuth.getCurrentUser().getEmail());
listMap.put(KEY_HAS_ACCESS , Arrays.asList(mAuth.getCurrentUser().getEmail()));
userRef.update("hasAccess" , FieldValue.arrayUnion(currListName));
gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-v4:28.0.0-rc02'
implementation 'com.android.support:design:28.0.0-rc01'
implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-auth:16.0.3'
implementation 'com.google.firebase:firebase-firestore:17.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.android.gms:play-services-auth:16.0.0'
}
their code:
DocumentReference washingtonRef = db.collection("cities").document("DC");
washingtonRef.update("regions", FieldValue.arrayUnion("greater_virginia"));
I found two FieldValue classes in Firebase Firestore SDK (21.0.0):
com.google.firebase.firestore.model.value.FieldValue
com.google.firebase.firestore.FieldValue
The first has no arrayUnion method, and second has it. Check what class is imported in your code file.
According to the release notes, array operations are supported starting with client SDK version 17.0.5. Here's a link to the release notes.
An any case, update your client SDK to use the latest Firestore APIs.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want use firebase in my app.
In my app I only use firebaseauth (without problems) but now i want use FirebaseFirestore (currently I don't have this in code yet) but when i add
implementation 'com.google.firebase:firebase-firestore:21.6.0'
in gradle my app start without errors and then crashes.
Where can i find the problem?
FirebaseUser user = mAuth.getCurrentUser();
DocumentReference df=fStore.collection("users").document(user.getUid());
Map<String,Object> userInfo= new HashMap<>();
userInfo.put("Email",mail);
userInfo.put("Name",name);
userInfo.put("Surname",surname);
userInfo.put("Phone",phone);
userInfo.put("Role","0");//0-user 1-admin 2- courier
df.set(userInfo);
build.gradle
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.navigation:navigation-fragment:2.1.0'
implementation 'androidx.navigation:navigation-ui:2.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.google.firebase:firebase-firestore:21.6.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
Your Firebase library versions don't match - Firebase Auth is too old compared to Firestore. If you add any library, you have to also have to make sure that all the other versions of the libraries are compatible with it. It's easiest to simply use the latest versions of everything that you see in the release notes. Minimally, you should upgrade firebase-auth to 19.4.0.
implementation 'com.google.firebase:firebase-auth:19.4.0'
implementation 'com.google.firebase:firebase-firestore:21.6.0'
I'm new to android and using firebase for an android app.
I have added the firebase realtime database by using the firebase assistant provided by android studio.
When I try proceeding with adding firebase code, android studio shows the error "cannot resolve..." with anything firebase related.
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
I have looked for a solution everywhere, re-added firebase multiple times and it still won't work. Of course, I have checked for the correct dependencies in the gradle scripts.
This is what it looks like currently:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-database:19.2.0'
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-firestore:21.3.1'
I would really appreciate some help. I might have missed something small due to being a beginner.
I was trying to add an AutoComplete feature while letting user select a place. I followed the official documents and implemented the feature. But if I just use the compatibility library ('com.google.android.libraries.places:places-compat:2.0.0') and don't use the new places library ('com.google.android.libraries.places:places:2.0.0') I cannot import anything useful related to AutoComplete.(only 4 classes from widget which are not useful for coding and I believe they cause the program type already present error when I include the new places library)
To start auto complete I use this code
val fields = Arrays.asList(Place.Field.ID,
Place.Field.NAME,
Place.Field.ADDRESS,
Place.Field.ADDRESS_COMPONENTS,
Place.Field.LAT_LNG,
Place.Field.OPENING_HOURS,
Place.Field.PHONE_NUMBER,
Place.Field.PRICE_LEVEL,
Place.Field.RATING,
Place.Field.TYPES,
Place.Field.PHOTO_METADATAS,
Place.Field.TYPES,
Place.Field.VIEWPORT,
Place.Field.USER_RATINGS_TOTAL)
val intent = Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN, fields)
.build(this#AddPartnerActivity)
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE)//Request code is just a number
}
Android doesn't suggest me any useful import while new places library is missing.When I use exclude in gradle Error: Program type already present: com.google.android.libraries.places.widget.AutocompleteActivityis not fixed. I can also paste my gradle file for your reference (it's a mess and I tried to write exclude into any import that can possibly create a problem). One important thing to note is code was working fine without adding the new places api if I don't use autocomplete.
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation ('com.google.android.gms:play-services-maps:16.0.0'){
exclude group: 'com.google.android.libraries.places.widget'
}
//Ion
implementation 'com.koushikdutta.ion:ion:2.+'
//Facebook Login
implementation 'com.facebook.android:facebook-login:4.41.0'
//Google Login
implementation 'com.google.android.gms:play-services-auth:16.0.1'
//Anko
implementation "org.jetbrains.anko:anko:$anko_version"
// Anko Layout
implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
implementation "org.jetbrains.anko:anko-commons:$anko_version"
implementation "org.jetbrains.anko:anko-design:$anko_version"
//Picasso
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'jp.wasabeef:picasso-transformations:2.2.1'
//PlaceholderView
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.mindorks.android:placeholderview:1.0.3'
kapt 'com.mindorks.android:placeholderview-compiler:1.0.3'
//Web Sockets
implementation 'com.koushikdutta.async:androidasync:2.+'
implementation 'com.squareup.okhttp3:okhttp:3.12.3'
//Gson
implementation 'com.google.code.gson:gson:2.8.5'
//Firebase Cloud Messaging
implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.firebase:firebase-database:19.0.0'
implementation 'com.google.android.play:core:1.5.0'
//Kotlin Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'
//Firebase Crashlytics
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
//Map Utility
implementation 'com.google.maps.android:android-maps-utils:0.5+'
//room
implementation 'androidx.room:room-runtime:2.0.0'
kapt 'androidx.room:room-compiler:2.0.0'
implementation 'com.google.android.gms:play-services-oss-licenses:16.0.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation ('com.schibstedspain.android:leku:6.2.1') {
exclude group: 'com.google.android.gms'
exclude group: 'androidx.appcompat'
exclude group: 'com.google.android.libraries.places.widget'
}
implementation ('com.google.android.libraries.places:places-compat:2.0.0'){
exclude group: 'com.google.android.libraries.places.widget'
}
implementation 'com.google.android.libraries.places:places:2.0.0')
apply plugin: 'com.google.gms.google-services'
My first mistake was that in compatibility library, I should use the Place prefix with AutoComplete to find the necessary imports.
val intent = PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).build(this#AddPartnerActivity)
Second issue was that I wanted to get the property opening_hours(and data about photos) from Placeobject which was present in new places library, but not present in compatibility library. In compatibility library to retrieve this, one should send a query instead of creating a list of types he wants to retrieve directly from the place object like I did in the question.
Basically one should sent a request to this url using the id retrieved from autocomplete. Documentation for this can be found here place details
val url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=$placeId&fields=opening_hours,photos,reviews&key=YOUR_API_KEY" //place id is the id of the place you got from autocomplete
My confusion came from the fact that you need to send a query in one library which I thnk is subject to billing while in the new api you can directly retrieve the opening hours.
My app doesn't contain any native code but I am using the following libraries :
Fireabse auth,database,storage,messaging
Google Maps, Location
SendGrid - for sending emails
RazorPay
Glide
When my app starts, the RAM taken by Native section is around 180 MB and after I open a fragment which reads data from the real-time database a couple of times, the RAM usage by Native section shoots up to 800 MB!
How can I reduce this?
Edit:
My dependencies
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-auth:16.1.0'
implementation 'com.google.firebase:firebase-database:16.0.6'
implementation 'com.google.firebase:firebase-storage:16.0.4'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.payumoney.sdkui:plug-n-play:1.5.0'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.razorpay:checkout:1.4.5'
implementation files('libs/sendgrid-0.1.2-jar.jar')
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.github.mklimek:frame-video-view:1.3.3'
Here's how I query data from the database
DatabaseReference carReference = FirebaseDatabase.getInstance().getReference();
carReference.child("carDetails").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
ArrayList<String> carsOwnedByUser = new ArrayList<>();
for (DataSnapshot carDetails : dataSnapshot.getChildren()) {
CarDetails car = carDetails.getValue(CarDetails.class);
if (userId.equals(car.getUserId())) {
carsOwnedByUser.add(car.getMake());
}
}
String selectedCar = sharedPreferences.getString(Constants.SHARED_PREFERENCES_SELECTED_CAR, "None");
if (carsOwnedByUser.size() > 0) {
if (selectedCar.equals("None")) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Constants.SHARED_PREFERENCES_SELECTED_CAR, carsOwnedByUser.get(0));
editor.apply();
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
What I have tried:
I have tried disabling the disk Caching in Glide and I have tried setting the DatabaseReferenec.getInstance().setPersistacneEnabled() and keepSynced() to false but they didn't help.
Edit 2:
I checked the heap dump in the profiler and saw that Bitmap was taking 300 MB in the native section
Image
First of all, below line is main reason for high memory usage.
carReference.child("carDetails"). addListenerForSingleValueEvent(...)
Above line load all carDetails's children from firebase database to device's memory. So avoid using that. If you still need to use that then paginate data using proper combination of startAt(), endAt(), limitToFirst() and limitToLast() query filters. Documentation
If you still unable to make correct pagination then go ahead and use proper database structure, so you can fetch required details using simple query. Best Practices for data structure
I wanna use RxJava binding APIs for Android UI widgets in my project.
Therefore following the guidance as per this site 'https://github.com/JakeWharton/RxBinding'
But I am unable to import any Android UI widgets in my Kotlin File.
Where as its working fine if I am consuming these widgets in Java File.
Hence, not been to find the actual of this issue.
For reference following are the gradle file and class files(both kotlin and java) using in same project
build.gradle
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.core:core-ktx:1.2.0-alpha01'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
//RxBinding
implementation 'com.jakewharton.rxbinding3:rxbinding:3.0.0-alpha2'
}
BindingExample.java class
RxBindingExample.kt class
Have tried exploring this issue on S.O. but questions or solutions are available for previous version of lib 'com.jakewharton.rxbinding2:rxbinding'
Check this reference:
https://github.com/JakeWharton/RxBinding/blob/master/rxbinding/src/main/java/com/jakewharton/rxbinding3/widget/TextViewTextChangeEventObservable.kt
There are different ways to use depending on the language, note the #file:JvmName("RxTextView") at the start. If you're using java the class RxTextView is visible, in kotlin you should use the extension functions provided by the lib, textChangeEvents() is an example of it.
like that, this will aquire the observable e.g.:
val someTextView = TextView(context)
someTextView.textChangeEvents()
Edit:
Seems that the old class that I've referenced was deleted, here is another one:
https://github.com/JakeWharton/RxBinding/blob/master/rxbinding/src/main/java/com/jakewharton/rxbinding4/widget/TextViewAfterTextChangeEventObservable.kt