Moving cloud firestore collection - firebase

I have a scenario in which I want to move my top level, in other words first, collection to the third level. Is the only way to go about this to use third party import-export software with manipulation to do this, or is there a firestore feature that I am missing?
All help will be greatly appreciated.
TopLevel>Documents>ThirdLevel, where top level is the first collection in the db. Just to give a better picture of what I am trying to say.

Firestore has no ability to move collections or documents. Instead, you can copy documents. You'll have to write code to read the contents of a document, then write those contents to a document in a new location, then delete the original document.

I know it's an old question, but just in case someone else needs this. It's now possible using google's cloud shell.
Details available in this link:
https://firebase.google.com/docs/firestore/manage-data/export-import
You have two options to export specific collections, modify them, then import them, or export the whole bucket, then import it back. As I understood you can't mix the operations for example export all and import specific collections.
Note: I believe you'll be charged for the read and write of all documents exported and imported. And billing must be activated.

maybe this can help
public void moveFirestoreDocument(DocumentReference fromPath, final DocumentReference toPath) {
fromPath.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null) {
toPath.set(document.getData())
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully written!");
fromPath.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully deleted!");
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w(TAG, "Error deleting document", e);
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
}
});
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
}

Related

Instance member 'setCurrentScreen' can't be accessed using static access

I am trying to add Firebase Analytics to my flutter app to display different screen names on the dashboard. I used a method that I found here on stackoverflow in this link:
How do I track Flutter screens in Firebase analytics?
I added this code in a separate file:
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:flutter/widgets.dart';
// A Navigator observer that notifies RouteAwares of changes to state of their Route
final routeObserver = RouteObserver<PageRoute>();
mixin RouteAwareAnalytics<T extends StatefulWidget> on State<T>
implements RouteAware {
AnalyticsRoute get route;
#override
void didChangeDependencies() {
routeObserver.subscribe(this, ModalRoute.of(context) as PageRoute<dynamic>);
super.didChangeDependencies();
}
#override
void dispose() {
routeObserver.unsubscribe(this);
super.dispose();
}
#override
void didPop() {}
#override
void didPopNext() {
// Called when the top route has been popped off,
// and the current route shows up.
_setCurrentScreen(route);
}
#override
void didPush() {
// Called when the current route has been pushed.
_setCurrentScreen(route);
}
#override
void didPushNext() {}
Future<void> _setCurrentScreen(AnalyticsRoute analyticsRoute) {
print('Setting current screen to $analyticsRoute');
return FirebaseAnalytics.**setCurrentScreen**(
screenName: screenName(analyticsRoute),
screenClassOverride: screenClass(analyticsRoute),
);
}
}
enum AnalyticsRoute { screenName }
String screenClass(AnalyticsRoute route) {
switch (route) {
case AnalyticsRoute.screenName:
return 'screenName';
}
throw ArgumentError.notNull('route');
}
String screenName(AnalyticsRoute route) {
switch (route) {
case AnalyticsRoute.screenName:
return '/screenName';
}
throw ArgumentError.notNull('route');
}
I then added the route observer in the screen that I want to track.
The setCurrentScreen method is not working well and the app is not building. Any suggestions on what I could do to build it.
From the FlutterFire documentation on screen tracking:
await FirebaseAnalytics.instance
.setCurrentScreen(
screenName: 'Products'
);

If I update more than one fields in an update using firestore, how many read operation that it is counted? [duplicate]

I have the following transaction using Firestore:
mDb.runTransaction(new Transaction.Function<Void>() {
#Override
public Void apply(final Transaction transaction) throws FirebaseFirestoreException {
DocumentReference documentReference = mDb.collection("collectionOne").document("documentOne");
/*
some code
*/
transaction.update(documentReference, App.getResourses().getString(R.string.field_one), FieldValue.increment(1));
transaction.update(documentReference, App.getResourses().getString(R.string.field_two), FieldValue.increment(1));
return null;
}
}).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d("Debugging", "Transaction correctly executed.");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w("Debugging", "Transaction failure.", e);
}
});
My question is: when updating, for example, two fields of the same document within the same transaction, will such a transaction yield to one or two documents reads?
when updating, for example, two fields of the same document within the same transaction, will such a transaction yield to one or two documents reads?
Doesn't matter how many fields you change in a document in one operation, you'll always be charged with one write operation. If you make the writes, one after the other, you'll be charged with two write operations.

How to know FirebaseRecyclerAdapter query is zero or not, exist or not

How to know FirebaseRecyclerAdapter query is zero or not, exist or not
I find some instructions on
https://github.com/firebase/FirebaseUI-Android/tree/master/database
it says:
Data and error events
When using the FirebaseRecyclerAdapter you may
want to perform some action every time data changes or when there is
an error. To do this, override the onDataChanged() and onError()
methods of the adapter:
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
// ...
#Override
public void onDataChanged() {
// Called each time there is a new data snapshot. You may want to use this method
// to hide a loading spinner or check for the "no documents" state and update your UI.
// ...
}
#Override
public void onError(DatabaseError e) {
// Called when there is an error getting data. You may want to update
// your UI to display an error message to the user.
// ...
}
};
When I tried to use as follow:
mAdapter = new FirebaseRecyclerAdapter<Place, PlaceViewHolder>(options)
{
#Override
public void onDataChanged(DataSnapshot dataSnapshot)
{
// Called each time there is a new data snapshot. You may want to use this method
// to hide a loading spinner or check for the "no documents" state and update your UI.
// ...
if (dataSnapshot.exists())
{
Log.d(TAG,"data exists");
}
else
{
Log.d(TAG,"No data exists");
}
}
#NonNull
#Override
public PlaceViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i)
{
Error message is:
Method does not override method from its superclass
So how can I solve this problem, Thank you.
I found the answer from another page:
https://www.programcreek.com/java-api-examples/?api=com.firebase.ui.database.FirebaseRecyclerAdapter
#Override
public void onDataChanged()
{
// Called each time there is a new data snapshot. You may want to use this method
// to hide a loading spinner or check for the "no documents" state and update your UI.
// ...
if (getItemCount() == 0)
{
Log.d(TAG,"No data exists");
}
else
{
Log.d(TAG,"data exists");
}
}

How can I go about uploading and receiving a users profile picture?

So far in my social media app, the user's data such as first name, last name, email, gender, and more can be saved in a firebase database and retrieved when needed. As of today, I got a working profile picture when first creating a profile you can tap on the empty profile picture icon and it loads up your gallery replacing it with whatever image the user chooses.
Although this is quite neat I need to be able to upload this image somehow under the Users node in my firebase database. I am quite lost when it comes to converting the bitmap data and after reading through some documentation it still confuses me. Below is my code for using a locally saved photo replacing it as the profile picture to show what I have so far.
#Override
public void onClick(View view)
{
if (view == profilePicture)
{
//Toast.makeText(this, "We made it to the onClick for image!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri targetUri = data.getData();
Bitmap bitmap;
try
{
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
profilePicture.setImageBitmap(bitmap);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The onClick method runs after the user taps the profile icon. Now I will show you the database I am currently working with, this is the Firebase real-time database, not the Firebase storage. While firebase storage might be more appropriate I can't seem to figure out how to tell whos photo would be who as it does not upload them with a user id associated with them.
Database Picture Here
Instead of uploading a bitMap, a far better solution for a simple problem like yours would be Firebase Storage, similar to the Database but you can upload files such as images with much ease.
Here are the methods I currently use in my app:
private void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
private void uploadImage() {
if(filePath != null)
{
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
StorageReference ref = storageReference.child("images/"+userID);
ref.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
toastMessage("Uploaded");
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
progressDialog.dismiss();
toastMessage("Failed"+e.getMessage());
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
#Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded "+(int)progress+"%");
}
});
}
}

How to cancel firebase async requests in flutter instead of checking mounted

I have a flutter app talking to the Firebase Realtime Database. I get the data asynchronously, obviously, but my UI allows the user to move to a different part of the app, which means by the time the request completes, the Widget may be unmounted. Best practices say to cancel the async work instead of checking the mounted property but I cannot seem to figure out how to do this for some reason.
#override void initState() {
super.initState();
firebaseRealtimeReference.child('myData').once().then((results) {
if (mounted) {
setState(() {
_myLocalData = results;
}
}
}
/* Alternately with async/await: */
_myLocalData = firebaseRealtimeDatabaseReference.child('myData').once();
}
#override Widget build(BuildContext context) {
return new MyWidget(_myLocalData);
}
#override dispose() {
// Instead of checking mounted in the future, I should instead
// cancel the work in progress here.
super.dispose();
}

Resources