Skip to content

Commit 722ae70

Browse files
authored
Add Firestore bundles snippets (#286)
1 parent 58b87db commit 722ae70

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.google.example.firestore.kotlin
2+
3+
import android.util.Log
4+
import com.google.firebase.firestore.FirebaseFirestore
5+
import com.google.firebase.firestore.Query
6+
import com.google.firebase.firestore.Source
7+
import java.io.IOException
8+
import java.io.InputStream
9+
import java.net.HttpURLConnection
10+
import java.net.URL
11+
12+
13+
/**
14+
* https://firebase.google.com/docs/firestore/solutions/serve-bundles
15+
*/
16+
abstract class SolutionBundles(private val db: FirebaseFirestore) {
17+
18+
// [START fs_bundle_load]
19+
@Throws(IOException::class)
20+
fun getBundleStream(urlString: String?): InputStream {
21+
val url = URL(urlString)
22+
val connection = url.openConnection() as HttpURLConnection
23+
return connection.inputStream
24+
}
25+
26+
@Throws(IOException::class)
27+
fun fetchFromBundle() {
28+
val bundleStream = getBundleStream("https://example.com/createBundle")
29+
val loadTask = db.loadBundle(bundleStream)
30+
31+
// Chain the following tasks
32+
// 1) Load the bundle
33+
// 2) Get the named query from the local cache
34+
// 3) Execute a get() on the named query
35+
loadTask.continueWithTask<Query> { task ->
36+
// Close the stream
37+
bundleStream.close()
38+
39+
// Calling .result propagates errors
40+
val progress = task.getResult(Exception::class.java)
41+
42+
// Get the named query from the bundle cache
43+
db.getNamedQuery("latest-stories-query")
44+
}.continueWithTask { task ->
45+
val query = task.getResult(Exception::class.java)!!
46+
47+
// get() the query results from the cache
48+
query.get(Source.CACHE)
49+
}.addOnCompleteListener { task ->
50+
if (!task.isSuccessful) {
51+
Log.w(TAG, "Bundle loading failed", task.exception)
52+
return@addOnCompleteListener
53+
}
54+
55+
// Get the QuerySnapshot from the bundle
56+
val storiesSnap = task.result
57+
58+
// Use the results
59+
// ...
60+
}
61+
}
62+
// [END fs_bundle_load]
63+
64+
companion object {
65+
private const val TAG = "SolutionBundles"
66+
}
67+
}

0 commit comments

Comments
 (0)