firebase-ios-sdk icon indicating copy to clipboard operation
firebase-ios-sdk copied to clipboard

Firestore Data Bundles doesn't support empty Array and empty Map

Open toco1001 opened this issue 4 years ago β€’ 20 comments

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

  1. [Cloud Function] Get my-home-users firestore query snapshot by using Firebase Admin SDK and bundle them into txt file.
  2. [Cloud Function] Upload the bundle txt file to GCS.
  3. [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()

toco1001 avatar Mar 02 '22 14:03 toco1001

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not have all the information required by the template. Looks like you forgot to fill out some sections. Please update the issue with more information.

google-oss-bot avatar Mar 02 '22 14:03 google-oss-bot

Hi,

I tried to reproduce the issue on my side and it returns successfully when there are Japanese characters inside bundle.txt. If you need more support, you can share your bundle text file here or email me at [email protected].

cherylEnkidu avatar Mar 04 '22 01:03 cherylEnkidu

Hi, @cherylEnkidu. Thanks for checking this issue. I've sent you an email with my file. Please check later.

toco1001 avatar Mar 04 '22 01:03 toco1001

Hi,

I look at the Latin1 encoded txt file you sent me and I think they are hand edited. Firestore data bundles can only support machine generated text file. Which means if you changing the txt content manually the functions wouldn't work. Hope it solves your question :)

cherylEnkidu avatar Mar 04 '22 16:03 cherylEnkidu

Hi,

Which means if you changing the txt content manually the functions wouldn't work.

Yes, I know. But I didn't edit the content manually.

I think this time the Gmail client app changes the content a bit. Please see below.

Attached

  • txt file (Latin1) in GCS
  • Cloud Function code for bundling firestore data

2022εΉ΄3月5ζ—₯(土) 1:57 cherylEnkidu @.***>:

Hi,

I look at the Latin1 encoded txt file you sent me and I think they are hand edited. Firestore data bundles can only support machine generated text file. Which means if you changing the txt content manually the functions wouldn't work. Hope it solves your question :)

β€” Reply to this email directly, view it on GitHub https://github.com/firebase/firebase-ios-sdk/issues/9407#issuecomment-1059338677, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADYTYKZ34WCHPTAK24HCNOLU6I6HHANCNFSM5PXR2MWQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

toco1001 avatar Mar 04 '22 22:03 toco1001

Hi, Thanks for reporting this issue. We have found the bug and we will work on to fix it.

cherylEnkidu avatar Mar 08 '22 16:03 cherylEnkidu

Great! I'm looking forward to it :)

toco1001 avatar Mar 09 '22 02:03 toco1001

Awesome!! Thank you @cherylEnkidu !!

toco1001 avatar Mar 24 '22 13:03 toco1001

@cherylEnkidu

Hi, Thank you for fix the issue. After updating the SDK to v8.1.5, I can load the bundle file in most cases.

However it's still failed when a document has empty Map.

If the document has empty Array, it's working. :)

toco1001 avatar Apr 15 '22 17:04 toco1001

Hi toco1001,

Thanks for your report, I will take a look at it and keep you updated :)

cherylEnkidu avatar Apr 19 '22 13:04 cherylEnkidu

Hi toco1001, I have verified the bug exists and logged it in the list, it will be fixed in the future. Internal tracking ticket b/229750482

cherylEnkidu avatar Apr 19 '22 20:04 cherylEnkidu

Hello @cherylEnkidu,

I am still seeing this issue over a year later. Is there any chance you can prioritize it soon?

Kal-Elx avatar May 23 '23 08:05 Kal-Elx

@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.

cherylEnkidu avatar May 23 '23 15:05 cherylEnkidu

Hello,

We have this issue as well, is there any hope this might get fixed soon?

freakness avatar Sep 03 '23 09:09 freakness

Hi , sorry for the delay, this bug fix hasn't been prioritized yet. I will update this ticket once it is fixed.

cherylEnkidu avatar Sep 05 '23 17:09 cherylEnkidu

Hi folks, This ticket is still in our backup log, the team's current policy is closing the external bug tracking ticket (github issue) and maintaining the internal bug tracking ticket(b/229750482). This github ticket will get notified when the bug is fixed.

cherylEnkidu avatar Jan 03 '24 17:01 cherylEnkidu

Hi,

The team decided to keep the ticket open until the feature is implemented or bug is fixed for external visibility.

cherylEnkidu avatar Jan 16 '24 22:01 cherylEnkidu

@cherylEnkidu , please consider prioritizing this issue! Thank you!

Spitzbua avatar Jan 17 '24 12:01 Spitzbua

Also waiting for a fix. After I added dummy data to every empty map and array for every document, the error has changed to

Failed to GetNextElement() from bundle with error Prefix string is not a valid number

I'm not sure if it is part of this bug or if I should open a new issue

NikolaiVisorApps avatar Jan 18 '24 16:01 NikolaiVisorApps

Thank you for every developers who reply to this thread. The fix is coming out in the next release.

cherylEnkidu avatar Jan 25 '24 21:01 cherylEnkidu