|
| 1 | +package com.google.example.firestore; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | + |
| 5 | +import androidx.annotation.NonNull; |
| 6 | + |
| 7 | +import com.google.android.gms.tasks.Continuation; |
| 8 | +import com.google.android.gms.tasks.OnCompleteListener; |
| 9 | +import com.google.android.gms.tasks.Task; |
| 10 | +import com.google.firebase.firestore.FirebaseFirestore; |
| 11 | +import com.google.firebase.firestore.LoadBundleTask; |
| 12 | +import com.google.firebase.firestore.LoadBundleTaskProgress; |
| 13 | +import com.google.firebase.firestore.Query; |
| 14 | +import com.google.firebase.firestore.QuerySnapshot; |
| 15 | +import com.google.firebase.firestore.Source; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.net.HttpURLConnection; |
| 20 | +import java.net.URL; |
| 21 | + |
| 22 | +/** |
| 23 | + * https://firebase.google.com/docs/firestore/solutions/serve-bundles |
| 24 | + */ |
| 25 | +public class SolutionBundles { |
| 26 | + |
| 27 | + private static final String TAG = "SolutionBundles"; |
| 28 | + |
| 29 | + private FirebaseFirestore db; |
| 30 | + |
| 31 | + // [START fs_bundle_load] |
| 32 | + public InputStream getBundleStream(String urlString) throws IOException { |
| 33 | + URL url = new URL(urlString); |
| 34 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 35 | + return connection.getInputStream(); |
| 36 | + } |
| 37 | + |
| 38 | + public void fetchBunldeFrom() throws IOException { |
| 39 | + final InputStream bundleStream = getBundleStream("https://example.com/createBundle"); |
| 40 | + LoadBundleTask loadTask = db.loadBundle(bundleStream); |
| 41 | + |
| 42 | + // Chain the following tasks |
| 43 | + // 1) Load the bundle |
| 44 | + // 2) Get the named query from the local cache |
| 45 | + // 3) Execute a get() on the named query |
| 46 | + loadTask.continueWithTask(new Continuation<LoadBundleTaskProgress, Task<Query>>() { |
| 47 | + @Override |
| 48 | + public Task<Query> then(@NonNull Task<LoadBundleTaskProgress> task) throws Exception { |
| 49 | + // Close the stream |
| 50 | + bundleStream.close(); |
| 51 | + |
| 52 | + // Calling getResult() propagates errors |
| 53 | + LoadBundleTaskProgress progress = task.getResult(Exception.class); |
| 54 | + |
| 55 | + // Get the named query from the bundle cache |
| 56 | + return db.getNamedQuery("latest-stories-query"); |
| 57 | + } |
| 58 | + }).continueWithTask(new Continuation<Query, Task<QuerySnapshot>>() { |
| 59 | + @Override |
| 60 | + public Task<QuerySnapshot> then(@NonNull Task<Query> task) throws Exception { |
| 61 | + Query query = task.getResult(Exception.class); |
| 62 | + |
| 63 | + // get() the query results from the cache |
| 64 | + return query.get(Source.CACHE); |
| 65 | + } |
| 66 | + }).addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { |
| 67 | + @Override |
| 68 | + public void onComplete(@NonNull Task<QuerySnapshot> task) { |
| 69 | + if (!task.isSuccessful()) { |
| 70 | + Log.w(TAG, "Bundle loading failed", task.getException()); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // Get the QuerySnapshot from the bundle |
| 75 | + QuerySnapshot storiesSnap = task.getResult(); |
| 76 | + |
| 77 | + // Use the results |
| 78 | + // ... |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + // [END fs_bundle_load] |
| 83 | + |
| 84 | +} |
| 85 | + |
0 commit comments