realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.76k stars 572 forks source link

Initializing a named sync Subscription leads to "undefined" #6689

Closed maierphilipp closed 1 month ago

maierphilipp commented 4 months ago

How frequently does the bug occur?

Always

Description

If I initialize a new named subscription using Expo SDK @50.0.17, Realm 12.8.1 and @realm/react: 0.6.1 the Subscription gets initialized and is working fine. But trying to access it by name is not possible. Loggin realm.subscriptions to the console leads to: {1: undefined, 2: undefined, ...}.

This my Provider:

import Realm from "realm";
import { ActivityIndicator } from "react-native";

import { RealmProvider, AppProvider, UserProvider } from "@realm/react";

// Models:
import { ShoppingItem, ShoppingList } from "../models/ShoppingItem";
import { Adress, PaymentInfo, Transactions, Users } from "../models/Users";
import { Followed } from "../models/Followed";
import { Follower } from "../models/Follower";

import { Recipe, Ingredient, Preparation } from "../models/Recipe";

// Components:
import Login from "../components/auth/login";

import { globalStyles } from "../constants/style";

const appId = "********";

export const RealmCustomProvider = ({ children }) => {
  const realmAccessBehavior = {
    type: "openImmediately",
  };

  return (
    <AppProvider id={appId}>
      <UserProvider fallback={Login}>
        <RealmProvider
          schema={[
            ShoppingList,
            ShoppingItem,
            Users,
            Followed,
            Follower,
            PaymentInfo,
            Transactions,
            Adress,
            Recipe,
            Ingredient,
            Preparation,
          ]}
          sync={{
            flexible: true,
            newRealmFileBehavior: realmAccessBehavior,
            existingRealmFileBehavior: realmAccessBehavior,
            // initialSubscriptions: {
            //   update: (subs, realm) => {
            //     subs.add(realm.objects("Users"));
            //   },
            //   rerunOnOpen: true,
            // },
            onError: console.error,
          }}
        >
          {children}
        </RealmProvider>
      </UserProvider>
    </AppProvider>
  );
};

And this is how the Query should perform on the ListScreen:

  const list = useQuery(ShoppingList, (collection) =>
    collection.filtered("_id == $0", id)
  );

  useEffect(() => {
    const createSubscription = async () => {
      await list.subscribe({ name: "listSub" });
    };

    createSubscription();
  }, [realm]);

I don't know, if the issue occurs because of initializing this synced realm for the query wrong, but I have tried several, in the different Docs recommended, Ways and everyone leads to the same solution.

Thanks in advance.

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

0.6.1

What services are you using?

Both Atlas Device Sync and Atlas App Services

Are you using encryption?

No

Platform OS and version(s)

Expo SDK @50.0.17, Realm 12.8.1 and @realm/react: 0.6.1

Build environment

Which debugger for React Native: ..

Cocoapods version

No response

sync-by-unito[bot] commented 4 months ago

➤ PM Bot commented:

Jira ticket: RJS-2827

kneth commented 4 months ago

@maierphilipp

Are you able to retrieve the subscription with realm.subscriptions.findByName("listSub")? Moreover it is interesting if the subscriptions have been download by checking realm.subscriptions.state.

maierphilipp commented 4 months ago

Thanks for your Response! I can now access the initial-sub with .findByName("listSub"). This hasn't worked before.

{"internal": {"createdAt": {Symbol(Realm.Timestamp.external_pointer): [Object]}, "id": "6657f074b58fcbdade05afba", "name": "listSub", "objectClassName": "ShoppingList", "queryString": "TRUEPREDICATE", "updatedAt": {Symbol(Realm.Timestamp.external_pointer): [Object]}}} gets logged to the console.

The State says "complete". I now encountered a new error. I am not able to subscribe to the query with the above code because .subscribe() is not a function but undefined. From where do I have to import it?

Sorry I really struggle with the subscriptions..

nirinchev commented 2 months ago

It's not clear to me how you've been able to create listSub if you say .subscribe is not a function? It sounds like at least once the code has run successfully in order to create the subscription. Can you create an isolated repro case for what you're seeing and share it here?

maierphilipp commented 2 months ago

@nirinchev as mentioned above I subscribed to my ShoppingList as an initialSub. This is not the solution I was expecting, as I have multiple initial subs right now, but I didn't get the problem solved otherwise. ".subscribe() is not a function".

nirinchev commented 2 months ago

The commented-out code for initialSubscriptions doesn't seem to assign a name, meaning it would create an unnamed subscription rather than one called listSub. In any case, without a clear repro case, it's kind of hard to debug things over github. The subscribe function is defined on Realm.Results - what is the type of list when createSubscription is invoked?

github-actions[bot] commented 1 month ago

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.