firebase-ios-sdk: Firestore Data Bundles doesn't support empty Array and empty Map
My environment
- Xcode version: 13.2.1
- Firebase SDK version: 8.12.1
- Installation method:
CocoaPods - Firebase Component: Firestore
- Target platform(s):
iOS
Problem
Steps to reproduce:
Context
To reduce firestore cost, I’m trying to use Firestore Data Bundles for my project.
Steps
- [Cloud Function] Get
my-home-usersfirestore query snapshot by using Firebase Admin SDK and bundle them into txt file. - [Cloud Function] Upload the bundle txt file to GCS.
- [iOS] Load the bundle txt file from GCS.
When I create bundle txt file with firestore query snapshot data which doesn’t include multi bytes string data I can get namedQuery in Swift.
However if I try to create bundle txt file which includes multi bytes string data like Japanese I failed to get it.
Relevant Code:
Cloud Functions
bundleHomeNewUsers.ts
import { getAdmin } from "../../util/firebase";
import * as firebase from "firebase-admin";
import * as fs from "fs";
export const bundleHomeNewUsers = async () => {
const BUCKET_NAME = "my-project-firestore-data-bundles";
const admin = getAdmin();
const db = admin.firestore();
const bundle = db.bundle("home-users");
const querySnapshot = await db
.collection("users")
.orderBy("createAt", "desc")
.limit(4)
.get();
const buffer = bundle.add("home-users-bundles-query", querySnapshot).build();
//Create tmp local file to upload
const bundledFilePath = `/tmp/bundle.txt`;
const charset = "utf8";
fs.writeFileSync(bundledFilePath, buffer, charset);
//Upload bundle file to Storage
const destination = `firestore-data-bundles/home_users_bundles.txt`;
await firebase
.storage()
.bucket(BUCKET_NAME)
.upload(bundledFilePath, {
destination,
public: true,
metadata: {
cacheControl: `public, max-age=60`,
contentType: "text/plain; charset=utf8",
},
});
console.log(
`Uploaded to https://storage.googleapis.com/${BUCKET_NAME}/${destination}`
);
};
Swift
FirestoreService.swift
let db = Firestore.firestore()
let urlString = "https://storage.googleapis.com/nadare-production-firestore-data-bundles/firestore-data-bundles/home_users_bundles.txt"
guard let url = URL(string: urlString),
let bundle = try? String(contentsOf: url, encoding: .utf8) else {
print("[DEBUG] loadHomeUsers: Not found bundle file")
return
}
db.loadBundle(Data(bundle.utf8)) { progress, error in
switch progress?.state {
case .success:
print("[DEBUG] success")
db.getQuery(named: "home-users-bundles-query") { query in
guard let query = query else {
print("[DEBUG] Failed to get named query")
return
}
print("[DEBUG] Succeeded to get query🎉🎉🎉")
query.getDocuments(source: .cache) { snapshot, error in
if let error = error {
return
}
guard let snapshot = snapshot else {
return
}
observer.sendCompleted()
}
}
case .inProgress:
print("[DEBUG] inProgress case")
case .error:
print("[DEBUG] error case: \(error.debugDescription)")
default:
print("[DEBUG] default case")
}
}
Output
[DEBUG] Failed to get named query
Firebase SDK error log
Error Domain=FIRFirestoreErrorDomain Code=2 "Loading bundle failed with unknown error" UserInfo={NSLocalizedDescription=Loading bundle failed with unknown error})
8.12.1 - [Firebase/Firestore][I-FST000001] Failed to GetNextElement() from bundle with error 'values' is missing or is not an array
8.12.1 - [Firebase/Firestore][I-FST000001] Progress set to Error, but error_status() is ok()
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (10 by maintainers)
Commits related to this issue
- [#9407]Enable Firestore data bundle to support empty array (#9467) * Change the require condition to optional in some data bundle decode functions — committed to firebase/firebase-ios-sdk by cherylEnkidu 2 years ago
Thank you for every developers who reply to this thread. The fix is coming out in the next release.
@cherylEnkidu , please consider prioritizing this issue! Thank you!
Hi,
The team decided to keep the ticket open until the feature is implemented or bug is fixed for external visibility.
@Kal-Elx Hi, there is no plan for fixing it recently. I will log your request in the tickets to help prioritize it in the future.
Awesome!! Thank you @cherylEnkidu !!